Android getting contacts photo from data.Email query

前端 未结 4 1137
半阙折子戏
半阙折子戏 2021-01-14 05:22

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 l

相关标签:
4条回答
  • 2021-01-14 06:02

    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.

    0 讨论(0)
  • 2021-01-14 06:04

    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.

    0 讨论(0)
  • 2021-01-14 06:06

    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/

    0 讨论(0)
  • 2021-01-14 06:19

    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

    0 讨论(0)
提交回复
热议问题