prabuddha_das
prabuddha_das

Reputation: 53

how can I set a ringtone for a specific contact in android in API level 21?

I know how to change phone ringtone, also how to get contacts, but how can I set a ringtone for a specific contact?

Uri contactData = ContactsContract.Contacts.CONTENT_URI;
String contactId = contactData.getLastPathSegment();

Cursor localCursor = managedQuery(contactData, PROJECTION, null, null, null);
localCursor.move(120/*CONTACT ID NUMBER*/);

String str1 = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));
String str2 = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));
Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, str1);
ContentValues localContentValues = new ContentValues();

localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE, Cob.selectedPath.get(0) /*DIRECT PATH TO MP3 File*/); 
getContentResolver().update(localUri, localContentValues, null, null);
Toast.makeText(this, "Ringtone assigned to: " + str2, 0).show();

Upvotes: 3

Views: 1315

Answers (1)

Stephan Branczyk
Stephan Branczyk

Reputation: 9385

Android has a special column for this: ContactsContract.CUSTOM_RINGTONE.

So, you could use ContactsContract.Contacts.getLookupUri to get your contact's Uri, after that pretty much all that's left is to call ContentResolver.update.

Originally from this StackOverflow answer.

Cob.selectedPath.get(0) /*DIRECT PATH TO MP3 File*/);

And no, a "direct path to an MP3 file" won't work. You must use the android api to get the path (just like it does in the StackOverflow answer I already linked to). Hopefully, you're not doing that despite what your comment says, but I can't say for sure because I don't know what is in your Cob.selectedPath.get(0).

Upvotes: 2

Related Questions