views:

330

answers:

1

Hi 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 contact'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.

can anyone help ?
(i refer to using API 2 of the contacts API on android SDK V5 and above )

10x.

A: 

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.

Pentium10
this is not what i meant ;-) i know this procedure...I want to use simpleCursorAdapter, i want o display in each row image and display name, moreover,i want only those contacts with postal address.I know the query, you just Query the ContactsContract.data table and filter out everything without mime type of postal address.It's of no problem to get the display name and _ID and even the contact_ID and the photo_id, but to use the simpleCursoradapter 'as is' (that is without inheritance and overriding the getView() function) i need to have a URI in the query answer, any suggestions ?
codeScriber
As far I know there is no way to get a reference to the photo, you have to stick with the URI.
Pentium10
that's what i though.10x.
codeScriber