3

以下代码用于更新图像,但抛出非法或错误值异常。任何机构都可以解决这个问题。

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());
4

2 回答 2

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();

你的数组列表添加代码应该是。

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());
于 2013-08-08T05:36:34.240 回答
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.
     }
 }
于 2013-08-07T20:55:54.030 回答