How to Update contact image using contact provider operation.

后端 未结 2 1811
情深已故
情深已故 2020-12-19 19:47

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.         


        
相关标签:
2条回答
  • 2020-12-19 19:56

    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());
    
    0 讨论(0)
  • 2020-12-19 20:10

    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.
         }
     }
    
    0 讨论(0)
提交回复
热议问题