Search code examples
androidandroid-contactsrawcontacts

Deleting a specific RawContact content directory entry


I'm new to android dev, so I may get the whole concept totally wrong. I want to delete a specific entry from RawContact directory entry. Here is code that I have:

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
         Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
         Cursor c = getContentResolver().query(entityUri,
                  new String[]{RawContacts._ID, Entity.DATA_ID, Entity.MIMETYPE,CommonDataKinds.GroupMembership.GROUP_SOURCE_ID},
                  null, null, null);

using cursor c I get appropriate Entity.DATA_ID. After that I try to delete an entry:

 getContentResolver().delete(entityUri,Entity.DATA_ID+"=?",
                  new String[]{id});

and get an error:

java.lang.UnsupportedOperationException: URI: content://com.android.contacts/raw_contacts/2709/entity

What am I doing wrong?

UPD 1 I am trying to remove group membership entry.


Solution

  • Looks like I was using the wrong URI. Also I switched to a "new" way of modifying the table:

     ArrayList<ContentProviderOperation> ops =
                          new ArrayList<ContentProviderOperation>();
    
                 ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
                          .withSelection(Data._ID + "=?", new String[]{i})
                          .build());
                 getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);