【问题标题】:update phone contact thumbnail更新电话联系人缩略图
【发布时间】:2014-11-30 12:54:37
【问题描述】:

问题:我无法通过相机拍摄的照片更新联系人的缩略图。 环境:模拟器,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();
                }

感谢任何关于为什么这有效(或者更确切地说不有效)的想法。

【问题讨论】:

    标签: android android-contentprovider android-contentresolver contactscontract


    【解决方案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);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多