以下代码用于更新图像,但它会抛出非法或错误的值异常。任何正文都可以解决此问题。
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
File f = new File(picturePath);
Uri photoUri = Uri.fromFile(f);
添加到数组列表编码
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(Photo.Photo_Uri,photoUri ).build());
答案 0 :(得分:4)
尝试以下
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
File f = new File(picturePath);
Uri photoUri = Uri.fromFile(f);
而不是替换以下内容。
Bitmap bitmap = ((BitmapDrawable) image.getDrawable())
.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
byte[] b = baos.toByteArray();
你的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(Photo.DATA15, b).build());
答案 1 :(得分:0)
这是一种方式:
public void writeDisplayPhoto(long rawContactId, byte[] photo) {
Uri rawContactPhotoUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
try {
AssetFileDescriptor fd =
getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw");
OutputStream os = fd.createOutputStream();
os.write(photo);
os.close();
fd.close();
} catch (IOException e) {
// Handle error cases.
}
}