I'm using the following code to set the ringtone for individual contacts
String contact_number = "MOBILE_NUMBER";
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, contact_number);
// The columns used for `Contacts.getLookupUri`
String[] projection = new String[]{
ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
};
// Build your Cursor
Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
if (data != null && data.moveToFirst()) {
data.moveToFirst();
try {
// Get the contact lookup Uri
long contactId = data.getLong(0);
String lookupKey = data.getString(1);
Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
if (contactUri == null) {
// Invalid arguments
return;
}
String ringtoneUri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Test";
File filePath = new File(ringtoneUri, "Bhar.MP3");
Log.e("filePath", filePath.toString());
String value = Uri.fromFile(filePath).toString();
// Apply the custom ringtone
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, value);
getContentResolver().update(contactUri, values, null, null);
} finally {
// Don't forget to close your Cursor
data.close();
}
}
this code was working in most of the devices but it's not working in Samsung j5 (Android version 6.0.1) and i also checked in the same version contained devices (like Moto G3), in those devices it's working fine.
Note:
I gave the all permissions related Marshmallow
When i'm assigning the ringtone to J5 mobile. It's assigning the ringtone name to the particular contact but it's not playing the assigned ringtone. For understanding check the attached image
