views:

88

answers:

2

ive got an imageview which im displaying a contacts picture using a uri which always looks similar to this:

content://com.android.contacts/contacts/34/photo

how would i be able to detect whether this photo exists, as if it doesnt then i want to use a placeholder instead (stored in my drawable folder). at the moment it just shows a blank image.

A: 

Possibly by using ContactsContract.Data.PHOTO_ID. If it doesn't have a value, then there is no photo.

Chiggins
thanks but ive allready figured out a workaround. will post it up when i get a chance
ng93
Please do, I'm interested in seeing what you did.
Chiggins
A: 

a function to get a contacts photo uri:

public Uri getPhotoUri(Integer contactid) {
    Cursor photoCur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'", null, ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC");
    photoCur.moveToPosition(contactid);
    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, photoCur.getLong(photoCur.getColumnIndex(ContactsContract.Contacts._ID)));
    Uri photo = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    return photo;
}

and calling that function (contactimage is an ImageView):

Uri contactphoto = getPhotoUri(2);
contactimage.setImageURI(contactphoto);
try {
    String nullString = contactimage.getDrawable().toString();
} catch (java.lang.NullPointerException ex) {
    contactimage.setImageResource(R.drawable.contactplaceholder);
}
ng93