使用查找URI在Android(API 8)中获取联系人照片

使用查找URI在Android(API 8)中获取联系人照片,android,api,uri,photo,Android,Api,Uri,Photo,我正在尝试使用其查找URI获取联系人图像。 我使用以下代码成功获取显示名称: Cursor c = context.getContentResolver().query(contactLookupUri, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null); 但是我没有找到一个方法来得到这张照片。Photo.Photo选项对于我正在使

我正在尝试使用其查找URI获取联系人图像。 我使用以下代码成功获取显示名称:

Cursor c = context.getContentResolver().query(contactLookupUri,
                new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null,
                null, null);
但是我没有找到一个方法来得到这张照片。Photo.Photo选项对于我正在使用的API无效,并且尝试使用inputStream获取该选项也不起作用(可能是我做错了什么):

谢谢,
Yoel

下面的函数返回联系人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);
}

另请参阅此

最终,我通过获取联系人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;
    }
}

你好,阿加瓦尔,谢谢你的回答,但我没有联系人id,我只有查找键。事实上,我不理解你的解决方案,因为你似乎根本没有使用查询结果。@yoel你能发布你的来源吗?我不明白“Uri contactLookupUri=getContactLookupUri(contactLookupKey);”查找键的解释可以在这里找到:
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;
    }
}