yoel
yoel

Reputation: 315

Get contact photo in Android (API 8) using LOOKUP_URI

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

Upvotes: 2

Views: 3270

Answers (2)

yoel
yoel

Reputation: 315

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;
    }
}

Upvotes: 2

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

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

Upvotes: 1

Related Questions