【问题标题】:Get contact photo in Android (API 8) using LOOKUP_URI使用 LOOKUP_URI 在 Android (API 8) 中获取联系人照片
【发布时间】:2012-04-30 04:47:19
【问题描述】:

我正在尝试使用其查找 URI 获取联系人图像。 我使用此代码成功获得了 DISPLAY_NAME:

Cursor c = context.getContentResolver().query(contactLookupUri,
                new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null,
                null, null);

但我没有找到获取照片的方法。 Photo.PHOTO 选项对我正在使用的 API 无效,并且尝试使用 inputStream 获取它也不起作用(也许我在那里做错了):

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

谢谢, 约尔

【问题讨论】:

    标签: android api uri photo


    【解决方案1】:

    最终我通过获取联系人 ID 并使用 inputStream 解决了这个问题:

    public static Uri getContactLookupUri(String contactLookupKey) {
            return Uri.withAppendedPath(
                    ContactsContract.Contacts.CONTENT_LOOKUP_URI, contactLookupKey);
        }
    
    public static Bitmap getContactImage(Context context, String contactLookupKey) {
    
        long contactId;
    
        try {
            Uri contactLookupUri = getContactLookupUri(contactLookupKey);
            Cursor c = context.getContentResolver().query(contactLookupUri,
                    new String[] { ContactsContract.Contacts._ID }, null, null,
                    null);
            try {
                if (c == null || c.moveToFirst() == false) {
                    return null;
                }
                contactId = c.getLong(0);
            } finally {
                c.close();
            }
    
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    
        Uri contactUri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, contactId);
        InputStream input = ContactsContract.Contacts
                .openContactPhotoInputStream(context.getContentResolver(),
                        contactUri);
    
        if (input != null) {
            return BitmapFactory.decodeStream(input);
        } else {
            return null;
        }
    }
    

    【讨论】:

    【解决方案2】:

    下面的函数返回你的contact_id的图片uri

    /**
     * @return the photo URI
     */
    public Uri getPhotoUri() {
        try {
            Cursor cur = this.ctx.getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND "
                            + ContactsContract.Data.MIMETYPE + "='"
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
                    null);
            if (cur != null) {
                if (!cur.moveToFirst()) {
                    return null; // no photo
                }
            } else {
                return null; // error in cursor process
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
                .parseLong(getId()));
        return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }
    

    也可以参考这个LINK

    【讨论】:

    • 您好 Agarwal,感谢您的回答,但我没有联系人 ID,我只有 lookupKey。而且我实际上不理解您的解决方案,因为您似乎根本没有使用查询结果。
    • @yoel 你能发布你的来源吗?我不明白“Uri contactLookupUri = getContactLookupUri(contactLookupKey);”
    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多