通过 LOOKUP_KEY 和 openContactPhotoInputStream 便捷方法加载联系人图片

发布于 2024-12-19 00:41:05 字数 713 浏览 4 评论 0原文

我正在修改我的应用程序,以便按照 API 文档的建议使用 LOOKUP_KEY 而不是 _ID 来存储联系人信息。我遇到的唯一问题是我无法再加载联系人的照片。

有问题的代码是这样的:

InputStream s = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), contactUri);

这返回以下错误: java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/lookup/1424i118.2312i1220228108/photo

contactUri我用作参数的参数是通过以下方式获取的: Uri contactUri = Uri.withAppishedPath(Contacts.CONTENT_LOOKUP_URI, contact_key);

在此示例中,contact_key1424i118.2312i1220228108

根据 API 文档,此帮助程序方法应该有效与 CONTENT_URICONTENT_LOOKUP_URI,我正在使用。

有什么想法吗?谢谢。

I'm modifying my app to store information on contacts using LOOKUP_KEY instead of _ID as suggested by the API docs. The only problem I'm having is that I'm no longer able to load the contact's photo.

The problematic code is this one:

InputStream s = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), contactUri);

This is returning the following error: java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/lookup/1424i118.2312i1220228108/photo

The contactUri that I am using as argument is acquired by the following: Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, contact_key);

and in this example, contact_key is 1424i118.2312i1220228108

Based on the API docs, this helper method should work with both CONTENT_URI or CONTENT_LOOKUP_URI, which I am using.

Any ideas? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-12-26 00:41:05

对于任何有类似问题的人来说,这对我来说很有效:

public Bitmap getPhoto(Uri uri){
    Bitmap photoBitmap = null;

    String[] projection = new String[] { ContactsContract.Contacts.PHOTO_ID };

    Cursor cc = getContentResolver().query(uri, projection, null, null, null);

    if(cc.moveToFirst()) {
        final String photoId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
        if(photoId != null) {
            final Cursor photo = managedQuery(
                    Data.CONTENT_URI,
                    new String[] {Photo.PHOTO},
                    Data._ID + "=?",
                    new String[] {photoId},
                    null
            );

            // Convert photo blob to a bitmap
            if(photo.moveToFirst()) {
                byte[] photoBlob = photo.getBlob(photo.getColumnIndex(Photo.PHOTO));
                photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
            }

            photo.close();
        }

    } 
    cc.close();

    return photoBitmap;
}

For anyone with a similar problem, this did the trick for me:

public Bitmap getPhoto(Uri uri){
    Bitmap photoBitmap = null;

    String[] projection = new String[] { ContactsContract.Contacts.PHOTO_ID };

    Cursor cc = getContentResolver().query(uri, projection, null, null, null);

    if(cc.moveToFirst()) {
        final String photoId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
        if(photoId != null) {
            final Cursor photo = managedQuery(
                    Data.CONTENT_URI,
                    new String[] {Photo.PHOTO},
                    Data._ID + "=?",
                    new String[] {photoId},
                    null
            );

            // Convert photo blob to a bitmap
            if(photo.moveToFirst()) {
                byte[] photoBlob = photo.getBlob(photo.getColumnIndex(Photo.PHOTO));
                photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
            }

            photo.close();
        }

    } 
    cc.close();

    return photoBitmap;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文