0

I am looking for a way to programatically update a contact's photo with a given Bitmap image. I'm doing the following, but nothing happens. Neither is the picture being set nor occurs an error.

Cursor cursor = provider.query(ContactsContract.RawContacts.CONTENT_URI,
                new String[]{ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.ACCOUNT_TYPE},
                ContactsContract.RawContacts.ACCOUNT_TYPE +"=?",
                new String[]{accountType},
                null);

        // Only one entry
        if(cursor.moveToFirst())
        {
            id = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
        }

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(CompressFormat.JPEG, 100, stream);
        byte[] bytes = stream.toByteArray();

        ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, Integer.parseInt(id))
                .withValue(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE)
                .withValue(Photo.PHOTO, bytes)
                .build());
    }
    provider.applyBatch(ops);

The image get loaded from a REST api, what seems to work properly since the bytes object actually is of the size of the downloaded image.

n1try
  • 1,050
  • 14
  • 23

1 Answers1

0

Your code connects photo data row to the specific raw contact, but it doesn't initiate any procedures that will change display photo for that raw contact and corresponding contact.

Use ContactsContract.RawContacts.DisplayPhoto for forced raw contact display photo editing. There is good usage example in the docs.

kuelye
  • 636
  • 7
  • 9