How to Update contact image using contact provider operation.

回眸只為那壹抹淺笑 提交于 2019-12-18 07:03:46

问题


The following code is used to update the image but it throws illegal or bad value exception.any body can solve this.

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
File f = new File(picturePath);
Uri photoUri = Uri.fromFile(f);

add to array list coding

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

回答1:


Try the following

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
File f = new File(picturePath);
Uri photoUri = Uri.fromFile(f);

insted of this replace the following.

Bitmap bitmap = ((BitmapDrawable) image.getDrawable())
                    .getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
byte[] b = baos.toByteArray();

and your array list adding code should be.

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



回答2:


Here is one way:

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.
     }
 }


来源:https://stackoverflow.com/questions/18112272/how-to-update-contact-image-using-contact-provider-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!