How to get Android Contact thumbnail

前端 未结 2 1839
面向向阳花
面向向阳花 2021-01-04 13:11

I have a listview adapter and I\'m trying the following in the newView method:

@Override
    public View newView(Context context, C         


        
相关标签:
2条回答
  • 2021-01-04 13:42
    private Uri getPhotoUriFromID(String id) {
        try {
            Cursor cur = getContentResolver()
                    .query(ContactsContract.Data.CONTENT_URI,
                            null,
                            ContactsContract.Data.CONTACT_ID
                                    + "="
                                    + id
                                    + " 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
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Uri person = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
        return Uri.withAppendedPath(person,
                ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }
    

    This is the function where you need to pass contact id and you will get the URI of the image which you can set easily in the imageview.

    use the response uri of this function like imageView.setImageURI(uri)

    Hope it will work for your code.

    0 讨论(0)
  • 2021-01-04 13:48

    Probably will help someone. Just leave it here.
    This way you can get thumbnail uri by contact id.
    Tested on Android API 28.

        ContentResolver cr = getContentResolver();
        String[] projection = new String[] {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
        String where = ContactsContract.Contacts.NAME_RAW_CONTACT_ID = "?";
        String[] selectionArgs = {your_contact_id}
        Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI, projection, where, selectionArgs, null);
    
        String thumbnailUri;
        if ((cur != null ? cur.getCount() : 0) > 0) {
            if (cur.moveToNext()) {
                thumbnailUri = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
            }
        }
        if(cur!=null){
            cur.close();
        }
        return thumbnailUri;
    
    0 讨论(0)
提交回复
热议问题