This method returns the photo Uri or null if does not exists for a contact identified by getId()
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;
}
If you add a projection to the Cursor to return ContactsContract.Data.CONTACT_ID, and ContactsContract.Data.DISPLAY_NAME, probably you will end up having a list of contacts that have photo set. (not all of the contacts). Then for each contact you can compute the Uri of the photo like in the beginning of the method.