2

I'm trying to get a contact image using its lookup URI. I succeed getting the DISPLAY_NAME using this code:

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

But I didn't find a way to get the photo. The Photo.PHOTO option is not valid for the API I'm using, and trying to get it using an inputStream didn't work as well (perhaps I did something wrong there):

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

Thanks, Yoel

yoel
  • 253
  • 3
  • 15

2 Answers2

2

Eventually I solved it by getting the contact ID and using an 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;
    }
}
yoel
  • 253
  • 3
  • 15
  • An explanation of the lookupKey can be found here: http://developer.android.com/resources/articles/contacts.html – yoel Apr 29 '12 at 16:02
1

The below function return the image uri of your contact_id

/**
 * @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);
}

Also refer this LINK

Community
  • 1
  • 1
Shankar Agarwal
  • 39,320
  • 8
  • 68
  • 66
  • Hi Agarwal,Thank for the answer but I don't have the contact id, I have only the lookupKey. And I actually don't understand your solution since it seems you're not using the results of the query at all. – yoel Apr 21 '12 at 08:30
  • @yoel can you post your source? I dont understand "Uri contactLookupUri = getContactLookupUri(contactLookupKey);" –  Apr 29 '12 at 09:14