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