0

Problem: I cannot update contact's thumbnail by picture taken from camera. Environment: emulator, api19

My code:

String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";

ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>();

                    String[] photoParams = new String[] { String.valueOf(contact.getPhoneContactId()),
                            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE };

                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(contentResolver, Uri.parse(contact.getPhoto()));
                    ByteArrayOutputStream image = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);

                    ops.add(android.content.ContentProviderOperation
                            .newUpdate(android.provider.ContactsContract.Data.CONTENT_URI).withSelection(where, photoParams)
                            .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray()).build());

contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);

Contact thumbnail is neither displayed in native contacts application, nor pulled programmatically via

ContentResolver cr = getActivity().getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

                Uri imageUri = null;

                if (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)) != null)
                    imageUri = Uri.parse(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)));
                InputStream is = null;
                try {
                    if (imageUri != null)
                        is = getActivity().getContentResolver().openInputStream(imageUri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

Appreciate any ideas on why this works (or rather not working).

NeviQ-OR
  • 305
  • 2
  • 12

1 Answers1

1

Use this function to get thumbnails:

 public static byte[] openPhoto(Activity activity, long contactId) {

    String imageWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
    String[] imageWhereParams = new String[]{Long.toString(contactId),
            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE};

    ContentResolver cr = activity.getContentResolver();
    Cursor imgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, imageWhere, imageWhereParams, null);
    byte[] image = null;
    if (imgCur.moveToFirst()) {
        image  = imgCur.getBlob(imgCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO));
        imgCur.close();
        return image;
    }
    imgCur.close();

    return null;
}

And this process to update them:

if(contact.isThumbnailInDb()){

            if(contact.getThumbnail() != null ) { //edit
                Log.e(TAG, "updating thumbnail");
                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(ContactsContract.Data.CONTACT_ID + " = ?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?",
                                new String[]{String.valueOf(contact.getContactId()), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE})
                        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, contact.getThumbnail())
                        .build());
            }
        } else {

            if(contact.getThumbnail() != null){
                Log.e(TAG, "inserting thumbnail");
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.getContactId())
                        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, contact.getThumbnail())
                        .build());
                contact.setThumbnailInDb(true);
            }
        }
Shasak
  • 790
  • 8
  • 19