Insert a custom ringtone in a rawcontact (new contact)

178 views Asked by At

I would like to add a custom ringtone in a rawcontact (before create the contact).

I used this code with succes to add a custom ringtone in a contact (already created):

String select = ContactsContract.Contacts._ID + "=?";
String[] args = new String[]{getAndroidId()};

changesToCommit.add(ContentProviderOperation.newUpdate(ContactsContract.Contacts.CONTENT_URI)
        .withSelection(select, args)
        .withValue(ContactsContract.Contacts.CUSTOM_RINGTONE, ringtone_uri_string)
        .build());

So now, i try to insert a custom ringtone in a rawcontact (a new contact not already created). I've tried with this code:

changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValueBackReference(ContactsContract.RawContacts._ID, 0)
                    .withValue(ContactsContract.RawContacts.CUSTOM_RINGTONE, ringtone_uri_string)
                    .build());

But it doesn't work. Any ideas ?

1

There are 1 answers

1
marmor On BEST ANSWER

CUSTOM_RINGTONE is a Contacts table field, not RawContacts.

You'd need to reference a contact-id, not a raw-contact-id, so I'm not sure that's possible while inserting a new raw contact.

EDIT

You're right, seems like I've missed that other CUSTOM_RINGTONE field in RawContacts. In that case, I think this should be a part of the first call to add the raw-contact, something like this:

ArrayList ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(RawContacts.ACCOUNT_TYPE, "my_account_type")
   .withValue(RawContacts.ACCOUNT_NAME, "my_account_name")
   .withValue(RawContacts.CUSTOM_RINGTONE, ringtone_uri_string)
   .build());
// Add multiple Data.CONTENT_URI rows here, e.g.:
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
   .withValueBackReference(Data.RAW_CONTACT_ID,  0)
   .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
   .withValue(StructuredName.DISPLAY_NAME, "Bob Dylan")
   .build());
...
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);