Android getting contacts photo from data.Email query

﹥>﹥吖頭↗ 提交于 2019-12-01 08:37:25

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.

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.

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/

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

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