3

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();
  • 1
    Please explain what the code you posted does and where the problem lies. Is it an attempt to solve the problem but not working? If yes, what exactly does not work? – Micky Feb 27 '15 at 11:53
  • This is the code for setting custom ringtone for a particular contact. But its not working. – prabuddha_das Feb 27 '15 at 12:00

1 Answers1

2

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).

Community
  • 1
  • 1
Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49