Here is the code i use but it doesn't remove the profile picture
String selection = ContactsContract.Data.MIMETYPE + " = ? AND "
+ ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID + " = ?";
String[] args = {ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE, getId()};
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
operations.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(selection, args).build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, operations);
} catch (OperationApplicationException | RemoteException e) {
e.printStackTrace();
}
Here is how i get the photo_file_id :
public String getId() {
String[] projection = {ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID};
String selection = ContactsContract.Data.DISPLAY_NAME + "=?";
String[] args = {name};
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection,
selection,
args,
null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
int index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID);
_id = cursor.getString(index);
} else {
Toast.makeText(this, "Some unknown error", Toast.LENGTH_SHORT).show();
}
return _id;
}
It doesn't remove the profile picture. I have tried the same code with PHOTO._ID and PHOTO.PHOTO_ID but both don't show any changes in profile picture.
Please help.
First of all, never assume a contact name is unique.
If your user has two or more contacts with the same name, you'll get a random photo in your getId()
method.
It's better to use the contact ID to get the photo.
For deleting the photo, I think you should simply delete the entire Data row.
To do that, when getting the Photo.PHOTO_FILE_ID
, you should also get the Data._ID
which is the row ID in the Data table representing this photo.
Then when deleting the photo do this:
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(Data._ID + " = " + dataRowId, null)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);