Android getting contacts photo from data.Email que

2019-02-19 08:11发布

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

4条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-19 08:53

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/

查看更多
时光不老,我们不散
3楼-- · 2019-02-19 08:55

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.

查看更多
我只想做你的唯一
4楼-- · 2019-02-19 08:57

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.

查看更多
老娘就宠你
5楼-- · 2019-02-19 09:04

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

查看更多
登录 后发表回答