我正在创建代码以编程方式更新Android Phone中的联系人图像。我尝试了许多方法,但是没有一种解决方案适合我。
我从上次活动中以byteArray
的形式获取图像内容,然后将其转换为Bitmap
,然后对其进行压缩,然后再次将其转换为ByteArray
,并将其传递给以上方法。
仍然没有运气。它不会更新所选联系人的照片。在这种情况下我做错了什么?
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 );
}
});