How to update a native contact photo on android?

2019-06-23 18:58发布

问题:

I am working on one sample application just to insert, update and delete the native android contact. I am able to successfully insert, update and delete the contact. But the problem in updating the contact photo. Below images are the observation where the same contact having two different issue.

After updating the contact, first image is still displaying the old image. But where as when i view the full details i am able to view the newly updated contact image as shown in the second image. Below is the code for updating the contact image.

 mBitmap =getAllowedPhotoBitmap(photo);
 mBitmap = ThumbnailUtils.extractThumbnail(mBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
    if(mBitmap!=null){    // If an image is selected successfully
        mBitmap.compress(Bitmap.CompressFormat.PNG ,100, stream);
      op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
      op.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " +               ContactsContract.Data.MIMETYPE + "=?", new String[{String.valueOf(native_contactid), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
     op.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, stream.toByteArray());
     ops.add(op.build());
}

What is the problem and where i am going wrong?

回答1:

Here is a open source app that does that: https://github.com/heinrisch/Contact-Picture-Sync/blob/master/src/heinrisch/contact/picture/sync/ContactHandler.java



回答2:

this file will help you set Image for contact with Contact ID

https://github.com/heinrisch/Contact-Picture-Sync/blob/master/src/heinrisch/contact/picture/sync/ContactHandler.java

void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        // Check for the request code, we might be usign multiple
        if (requestCode == PICK_CONTACT_REQUEST) {
            Uri contactUri = data.getData();                
            String[] projection = {Phone.CONTACT_ID,Phone.NUMBER,ContactsContract.Data.RAW_CONTACT_ID,ContactsContract.Data._ID };
            Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            int columcontactID = cursor.getColumnIndex(Phone.CONTACT_ID);
            String contactID = cursor.getString(columcontactID);

            Bitmap item = (imgBg.getVisibleRectangleBitmap());

            setContactPicture(AtWallpaperDetails.this, contactID, item);
        }
    }
}