Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a contact picture inside ContentProviderOperation?

With the snippet below, I am able to sync all my contact with a REST API. That's great and everything works fine. I can add people with name and phone number.

Unfortunately, I am now trying without success to add a picture from SDCard ( I get a Bitmap or a Drawable)

Can someone point me a way to achieve this or give me some clues?

Thank a lot!

here is the sample code I currently use:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(RawContacts.ACCOUNT_TYPE, null)
   .withValue(RawContacts.ACCOUNT_NAME,null )
   .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
   .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
   .build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
   .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
   .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
   .build());  
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
like image 725
Waza_Be Avatar asked Dec 12 '11 12:12

Waza_Be


1 Answers

Please try this

Bitmap bmImage = BitmapFactory.decodeFile(imagePath);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
                    bmImage.compress(Bitmap.CompressFormat.JPEG, 80, baos);   
                    byte[] b = baos.toByteArray();



                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                            .withValue(ContactsContract.Data.MIMETYPE,
                                    ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                                    .withValue(ContactsContract.CommonDataKinds.Photo.DATA15,b) 
                                    .build());
like image 143
Sandy Avatar answered Nov 14 '22 23:11

Sandy