Android getting contacts photo from data.Email query

∥☆過路亽.° 提交于 2019-12-01 05:39:58

问题


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);


回答1:


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.




回答2:


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.




回答3:


maybe you can take a look at this blog post in the example there they query all contacts, email addresses and the contact photo http://blog.app-solut.com/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/




回答4:


best code will be

Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Id);

    Bitmap photoBitmap;
    ContentResolver cr = getContentResolver();
    InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, photoUri);

    photoBitmap = BitmapFactory.decodeStream(is);

it works for all



来源:https://stackoverflow.com/questions/5332333/android-getting-contacts-photo-from-data-email-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!