views:

47

answers:

1

I want to insert a contact photo with other information in a batch insert. "is" is the input stream using the uri of the photo:

is = Data.clientContext.getContentResolver().openInputStream(/data/data/com.project.xxxxxxxxxxxxx/files/photo); 

            op_list.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.PHOTO, is)
                .build());

I cannot seem to get the photo to insert with the batch. Any pointers?

+1  A: 

this method can resolve your problem.

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValue(Data.RAW_CONTACT_ID, rawId)
                        .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray())
                        .build());
additional explanations : photo is Bitmap type,Bitmap photo;