更新手机联系人缩图

时间:2014-11-30 12:54:38

标签: android android-contentprovider android-contentresolver contactscontract

问题:我无法通过相机拍摄的照片更新联系人的缩略图。 环境:模拟器,api19

我的代码:

String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";

ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>();

                    String[] photoParams = new String[] { String.valueOf(contact.getPhoneContactId()),
                            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE };

                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(contentResolver, Uri.parse(contact.getPhoto()));
                    ByteArrayOutputStream image = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);

                    ops.add(android.content.ContentProviderOperation
                            .newUpdate(android.provider.ContactsContract.Data.CONTENT_URI).withSelection(where, photoParams)
                            .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray()).build());

contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);

联系人缩略图既不会显示在本机联系人应用程序中,也不会通过

以编程方式提取
ContentResolver cr = getActivity().getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

                Uri imageUri = null;

                if (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)) != null)
                    imageUri = Uri.parse(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)));
                InputStream is = null;
                try {
                    if (imageUri != null)
                        is = getActivity().getContentResolver().openInputStream(imageUri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

欣赏有关其原因(或更确切地说无效)的任何想法。

1 个答案:

答案 0 :(得分:1)

使用此功能获取缩略图:

 public static byte[] openPhoto(Activity activity, long contactId) {

    String imageWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
    String[] imageWhereParams = new String[]{Long.toString(contactId),
            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE};

    ContentResolver cr = activity.getContentResolver();
    Cursor imgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, imageWhere, imageWhereParams, null);
    byte[] image = null;
    if (imgCur.moveToFirst()) {
        image  = imgCur.getBlob(imgCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO));
        imgCur.close();
        return image;
    }
    imgCur.close();

    return null;
}

这个更新它们的过程:

if(contact.isThumbnailInDb()){

            if(contact.getThumbnail() != null ) { //edit
                Log.e(TAG, "updating thumbnail");
                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(ContactsContract.Data.CONTACT_ID + " = ?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?",
                                new String[]{String.valueOf(contact.getContactId()), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE})
                        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, contact.getThumbnail())
                        .build());
            }
        } else {

            if(contact.getThumbnail() != null){
                Log.e(TAG, "inserting thumbnail");
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.getContactId())
                        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, contact.getThumbnail())
                        .build());
                contact.setThumbnailInDb(true);
            }
        }