Reputation: 11
I am creating a code to update the Image of a Contact in my Android Phone programmatically. I have tried many methods but none of the solutions worked for me.
I am getting the image content as a byteArray
from my last activity & then converting it into a Bitmap
& them compress it & then again converting it into ByteArray
& passing it to the above method.
Still no luck. Its not updating the photo of the selected contact. What a I doing wrong in this scenario?
This is my method
public void changeContactImage(String contactId, byte[] b_array) {
Log.d("Bitmap_Arrrrray", b_array.toString());
ArrayList<ContentProviderOperation> ops = new ArrayList < > ();
ops.add(ContentProviderOperation
.newUpdate(
ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {
contactId,
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.DATA15, b_array).build());
.withValue(ContactsContract.Contacts.Photo.DATA15, b_array).build());
try {
getApplicationContext().getContentResolver().
applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
Log.d("RemoteException", e.toString());
} catch (OperationApplicationException e) {
Log.d("OperationException", e.toString());
}
}
Calling the above method will be like
Bitmap_Array = getIntent().getByteArrayExtra("contact_image");
bmp = BitmapFactory.decodeByteArray(Bitmap_Array, 0, Bitmap_Array.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, baos);
TMP_Bitmap_Array = baos.toByteArray();
LV_Contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// bitmap = (Bitmap)getIntent().getParcelableExtra("Bitmap");
// save_to_contact(bitmap);
String C_Id = String.valueOf(parent.getItemIdAtPosition(position));
changeContactImage(C_Id, Bitmap_Array );
}
});
Upvotes: 0
Views: 508