tags:

views:

137

answers:

1

I am assigning to an ImageView contacts images using this code:

mPhotoView = (ImageView) findViewById(R.id.photo);
mPhotoView.setImageURI(objItem.getPhotoUri());

If the contact has no image, this does nothing, and no error is raised.

When there is no image, I want to add a default image. So I need to check either if the image was added to the view, or check that the URI holds some image data

How do I achieve that?

Than I will set default image by this:

mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
+5  A: 

If your target device is running android 2.0/2.0.1/2.1 you will have to query ContactsContract.Data.CONTENT_URI with a selection like:

Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE

Otherwise query Contacts.Photos.CONTENT_URI

Edit by Pentium10

For reference I include here the method I come up with (if you still see bugs, update it):

public Uri getPhotoUri() {
    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getId()));
    Uri photo = Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

    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
    }
    return photo;
}
Schildmeijer
This is not a detailed explanation, but if you are familiar with ContentProviders/ContentResolvers this should be enough. Otherwise let me know and I'll post some code examples.
Schildmeijer
Thank you. I edited your answer and included the solution I come up with. Check for bugs.
Pentium10