Henrique Henrique - 1 year ago 110
Android Question

Loading contact picture via LOOKUP_KEY and openContactPhotoInputStream convenience method

I'm modifying my app to store information on contacts using LOOKUP_KEY instead of _ID as suggested by the API docs. The only problem I'm having is that I'm no longer able to load the contact's photo.

The problematic code is this one:

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

This is returning the following error:
java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/lookup/1424i118.2312i1220228108/photo

The contactUri that I am using as argument is acquired by the following:
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, contact_key);

and in this example,

Based on the API docs, this helper method should work with both
, which I am using.

Any ideas? Thanks.

Answer Source

For anyone with a similar problem, this did the trick for me:

public Bitmap getPhoto(Uri uri){
    Bitmap photoBitmap = null;

    String[] projection = new String[] { ContactsContract.Contacts.PHOTO_ID };

    Cursor cc = getContentResolver().query(uri, projection, null, null, null);

    if(cc.moveToFirst()) {
        final String photoId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
        if(photoId != null) {
            final Cursor photo = managedQuery(
                    new String[] {Photo.PHOTO},
                    Data._ID + "=?",
                    new String[] {photoId},

            // Convert photo blob to a bitmap
            if(photo.moveToFirst()) {
                byte[] photoBlob = photo.getBlob(photo.getColumnIndex(Photo.PHOTO));
                photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);



    return photoBitmap;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download