Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

android - How to Update contact image using contact provider operation.

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());
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...