Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getting contacts photo from data.Email query

I am making a autocomplete Field that queries contacts by Display name and Email. When someone clicks on the desired contact after the filtering that contact is added to a list with his email, display name and Picture if he has any.

So so far i have managed to do everything except to make the Photo appear. Here is how i run the query to get the email, display name, ID , and Photo ID.

    return mContent.query(Email.CONTENT_URI,
              PROJECTION, filter, null, null);

where projection is:

 PROJECTION = new String[] {ContactsContract.Contacts._ID,
              ContactsContract.Contacts.DISPLAY_NAME,
              ContactsContract.Contacts.PHOTO_ID,
                Email.DATA
            };

This one does what i need and returns all the data. But one thing i noticed during debugging this issue is that the contact id is different than if you run the query against ContactsContract.Contacts.CONTENT_URI for a specific display name for example.

For example the tests i have run where i get all the contacts by running the Contacts.CONTENT_URI gave me a contact with an image and Id of 152. However the query against the Email.CONTENT_URI gives me an id of 452 for the same contact (With same display name and email address). so when i try to get the Photo for a content uri containing the Id 452 it returns that the photo doesnt exist, but if i try to get the photo for 152 it works perfectly.

What is causing this issue? how do i get the correct User ID? Is there any relational query that i can maybe run to get a contact ID, or maybe a correct way to get it with the help of this one.

Thank you.

EDIT

I found this digging around old code. Might be helpful to anyone.

So the full query:

String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
            Email.DATA, ContactsContract.CommonDataKinds.Photo.CONTACT_ID };

                String order = " CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
                        + " NOT LIKE '%@%' THEN 1" + " ELSE 2 END, "
                        + ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE";
                String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;

then its

getContentResolver().query( Email.CONTENT_URI, PROJECTION, filter, null, order);

like image 961
DArkO Avatar asked Dec 15 '25 12:12

DArkO


2 Answers

When you want to access the photo of a contact, you need to specify the contact photo URI, for example using this method:

public Uri getContactPhotoUri(long contactId) {
    Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    photoUri = Uri.withAppendedPath(photoUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    return photoUri;
}

But for contactId you must use:

String id = ContactsContract.CommonDataKinds.Photo.CONTACT_ID;
long contactId = Long.parseLong(id);

Please note that a common error is to use ContactsContract.Contacts._ID instead ContactsContract.CommonDataKinds.Photo.CONTACT_ID

I hope that can help you.

like image 89
ASMaitre Avatar answered Dec 17 '25 02:12

ASMaitre


You should use RAW_CONTACT_ID in the query. For ex, there can be two different contacts i.e. different RAW_CONTACT_ID for a single CONTACT_ID.

like image 45
Karan Avatar answered Dec 17 '25 02:12

Karan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!