2

ルックアップ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);

ありがとう、Yoel

4

2 に答える 2

2

最終的に、連絡先 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;
    }
}
于 2012-04-21T08:50:01.040 に答える
1

以下の関数は、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);
}

こちらのリンクもご覧ください

于 2012-04-18T17:59:24.110 に答える