Contacts query with name and picture URI

后端 未结 1 547
遥遥无期
遥遥无期 2021-01-03 16:34

I couldn\'t find a single query that would give me in API 2.0 of the contacts API the URI of the contact\'s image and the display name.

For now as far as i know i

相关标签:
1条回答
  • 2021-01-03 16:53

    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.

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