我正在创建一个代码来以编程方式更新我的 Android 手机中的联系人图像。我尝试了很多方法,但没有一个解决方案对我有用。
我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 );
}
});