PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to delete profile picture of a contact in android programmatically?

 October 21, 2022     android, android-contacts, android-contentprovider, contacts     No comments   

Issue

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.


Solution

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


Answered By - marmor
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing