views:

2522

answers:

3

I'm having trouble loading a photo for a contact in Android. I've googled for an answer, but so far have come up empty. Does anyone have an example of querying for a Contact, then loading the Photo?

So, given a contactUri which comes from an Activity result called using

startActivityForResult(new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI),PICK_CONTACT_REQUEST) 

is:

content://com.android.contacts/data/1557

The loadContact(..) works fine. However when I call the getPhoto(...) method, I get a null value for the photo InputStream. It is also confusing because the URI values are different. The contactPhotoUri evaluates to:

content://com.android.contacts/contacts/1557

See the comments inline in the code below.

class ContactAccessor {

/**
 * Retrieves the contact information.
 */
public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {

    //contactUri --> content://com.android.contacts/data/1557

    ContactInfo contactInfo = new ContactInfo();

    // Load the display name for the specified person
    Cursor cursor = contentResolver.query(contactUri,
                                        new String[]{Contacts._ID, 
                                                     Contacts.DISPLAY_NAME, 
                                                     Phone.NUMBER,
                                                     Contacts.PHOTO_ID}, null, null, null);
    try {
        if (cursor.moveToFirst()) {
            contactInfo.setId(cursor.getLong(0));
            contactInfo.setDisplayName(cursor.getString(1));
            contactInfo.setPhoneNumber(cursor.getString(2));
        }
    } finally {
        cursor.close();
    }        
    return contactInfo;  // <-- returns info for contact
}

public Bitmap getPhoto(ContentResolver contentResolver, Long contactId) {
    Uri contactPhotoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);

    // contactPhotoUri --> content://com.android.contacts/contacts/1557

    InputStream photoDataStream = Contacts.openContactPhotoInputStream(contentResolver,contactPhotoUri); // <-- always null
    Bitmap photo = BitmapFactory.decodeStream(photoDataStream);
    return photo;
}

public class ContactInfo {

    private long id;
    private String displayName;
    private String phoneNumber;
    private Uri photoUri;

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public Uri getPhotoUri() {
        return this.photoUri;
    }

    public void setPhotoUri(Uri photoUri) {
        this.photoUri = photoUri;
    }

    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

}

}

Clearly, I'm doing something wrong here, but I can't seem to figure out what the problem is. Thanks.

+1  A: 

It seems that my problem was because the contacts in my device were synced from facebook, and the photo is therefore not available.

http://groups.google.com/group/android-developers/msg/be8d0cf3928e4b7f

PaulH
and why the default contact application shows the facebook photo?
krtek
A: 

I am also that problem, agoning me. do you check a number as you input a number 2nd parameter for all case (ex:for statement) . I was settled the problme at that method.

rnp_dgun
A: 

So is there no decent solution? If the contacts use facebook photos, we're SOL?

Scott Main
I wasn't able to find a way to load photos from facebook. I don't know if it changed in Froyo or not.
PaulH