Contacts query with name and picture URI
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 can create a URI by having the con开发者_如何学运维tact's _ID , but i didn't see any row name that i can use in the projection of Data or Contact to get all that i need.
(i refer to using API 2 of the contacts API on android SDK V5 and above )
10x.
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.
精彩评论