问题
I'm developing music player, in that i have to set audio file as ringtone for contacts. Below code is working for some phones and some phones not.
public static int setContactRingtone(@NonNull Context context, @NonNull Song song, String number) {
int value = 0;
if (context == null) {
value = 0;
}
File f = new File(song.getmSongPath());
final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
final String[] projection = new String[] {
ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
};
final Cursor data = context.getContentResolver().query(lookupUri, projection, null, null, null);
if (data != null && data.moveToFirst()) {
final long contactId = data.getLong(0);
final String lookupKey = data.getString(1);
final Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
if (contactUri != null) {
final String valuex = Uri.fromFile(f).toString();
final ContentValues values = new ContentValues(1);
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, valuex);
if (contactUri != null) {
int ooo = context.getContentResolver().update(contactUri, values, null, null);
if (ooo > 0){
value = 1;
}
}
}
data.close();
}
return value;
}
来源:https://stackoverflow.com/questions/59020167/custom-contact-ringtone-not-working-on-some-devices