detect if contact has photo

前端 未结 2 1687
时光取名叫无心
时光取名叫无心 2021-02-09 00:14

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

相关标签:
2条回答
  • 2021-02-09 00:40

    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);
    }
    
    0 讨论(0)
  • 2021-02-09 00:43

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

    0 讨论(0)
提交回复
热议问题