detect if contact has photo

2020-06-03 07:12发布

I've got an ImageView which I'm 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 doesn't then I want to use a placeholder instead (stored in my drawable folder). At the moment it just shows a blank image.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-06-03 07:43

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);
if (contactimage.getDrawable() == null) {
    contactimage.setImageResource(R.drawable.contactplaceholder);
}
查看更多
Deceive 欺骗
3楼-- · 2020-06-03 08:08

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

查看更多
登录 后发表回答