如何在ContentProviderOperation中添加联系人图片?

时间:2022-06-01 19:27:14

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.

通过下面的代码段,我可以将所有联系人与REST API同步。这很好,一切正常。我可以添加姓名和电话号码的人。

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

不幸的是,我现在正在努力尝试从SDCard添加图片(我得到一个Bitmap或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);

1 个解决方案

#1


12  

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

#1


12  

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