在ContactsContract中更新照片的功能 [英] Function to update a photo in ContactsContract

查看:113
本文介绍了在ContactsContract中更新照片的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用一个具有联系人ID和图片uri的函数来更新ContactsContract中的一张照片,但它似乎不起作用(并且我的函数返回true).

I try to update a photo in ContactsContract with a function who take the id of the contact and the uri of the picture but it seem it's not working (and my function return true).

我真的不明白,因为代码看起来不错.

I really don't understand because the code look good.

当联系人已经有照片时,它似乎正在工作...

It seem it's working when the contact have already a photo...

这是我的功能:

boolean updatePhoto(String idStr, String uri){
        if (uri != null) {
            ArrayList<ContentProviderOperation> ops = new ArrayList<>();

            File imgFile = new File(uri.replace("file://", ""));
            if (imgFile.exists()) {

                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                myBitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(ContactsContract.Data.CONTACT_ID + " = ?" + " AND " + ContactsContract.Data.MIMETYPE + "=?",
                                new String[]{idStr, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE})
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, stream.toByteArray())
                        .build());

                try {
                    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                } catch(Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }

推荐答案

您的代码仅在联系人已经有照片的情况下才有效,因为您使用的是ContentProviderOperation.newUpdate;如果联系人没有照片,则需要使用ContentProviderOperation.newInsert.

Your code only works if the contact already has a photo because your using ContentProviderOperation.newUpdate, if the contact doesn't have a photo you'll need to use ContentProviderOperation.newInsert.

您需要先查询联系人以查看是否有照片,然后更新/插入新照片:

You need to first query the contact to see if it has a photo, and then update/insert the new photo:

private boolean hasPhoto(long contactId) {
    Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
    InputStream input = Contacts.openContactPhotoInputStream(getContentResolver(), uri);
    if (input == null) {
        return false;
    }
    Bitmap photo = BitmapFactory.decodeStream(input);
    return (photo != null);
}

private long getRawId(long contactId) {
    String selection = RawContacts.CONTACT_ID + "='" + contactId + "'";
    Cursor cur = contentResolver.query(RawContacts.CONTENT_URI, new String[]{ RawContacts._ID }, selection, null, null);
    try {
        if (cur.moveToNext()) {
            return cur.getLong(0);
        }
    } finally {
        cur.close();
    }
    return 0;
}

private boolean updatePhoto(long contactId, String uri) {
    if (uri == null) {
        // do nothing?
        return false;
    }

    ContentProviderOperation.Builder builder;
    if (hasPhoto(contactId)) {
        builder = ContentProviderOperation.newUpdate(Data.CONTENT_URI);
        builder.withSelection(Data.CONTACT_ID + " = ?" + " AND " + Data.MIMETYPE + "=?",
                    new String[]{ String.valueOf(contactId), Photo.CONTENT_ITEM_TYPE});
    } else {
        long rawId = getRawId(contactId);
        builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
        builder.withValue(Data.RAW_CONTACT_ID, rawId);
    }

    builder.withValue(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
    builder.withValue(Data.IS_SUPER_PRIMARY, 1);
    builder.withValue(Data.IS_PRIMARY, 1);

    byte[] photo = getPhotoAsByteArray(uri); // to simplify the answer's code
    builder.withValue(Photo.PHOTO, photo);

    try {
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();
        ops.add(builder.build());
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch(Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

这篇关于在ContactsContract中更新照片的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆