0

Here is my code to update contact and set the custom ring tune it is running successfully but it is not working and I'm using API 9

        sound = new File(folder,SONG);
                ContentValues values = new ContentValues();
                values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
                values.put(MediaStore.MediaColumns.TITLE, "1-800-hotlinebling");
                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                values.put(MediaStore.Audio.Media.ARTIST, "Umair Ali");
                values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                values.put(MediaStore.Audio.Media.IS_ALARM, true);
                values.put(MediaStore.Audio.Media.IS_MUSIC, true);
                values.put(ContactsContract.Data.RAW_CONTACT_ID, contactID);
                values.putNull(ContactsContract.Data.CUSTOM_RINGTONE);
                Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactID);
                getContentResolver().update(localUri, values, null, null);

1 Answers1

0

In StackOverflow similar issue Setting contact custom ringtone, how? you would find that answer

I found out how it works. Below you can see the fixed code code:

    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,
  f.getAbsolutePath()+"/Adventure.ogg");
  getContentResolver().update(localUri, localContentValues, null, null);      
  Toast.makeText(this, "Ringtone assigned to: " + str2, 0).show();

Just change the contact id number to the id of the contact you want to change.

Hope it help

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94