Skip to content Skip to sidebar Skip to footer

How Do You Get A Contact Photo Through The Contact Provider In Android 2.3.6?

I have this version that works for android 4 (String email is a gmail address): private Uri getPhotoUriFromEmail(String email) { Uri u = null; String[] projection = { Conta

Solution 1:

You can get the contact photo uri without using ContactsContract.CommonDataKinds.Email.PHOTO_URI this way:

private Uri getPhotoUriFromEmail(String email) {
    Uriu=null;
    String[] projection = { ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String photoUri;
    ContentResolvercr= getContentResolver();
    CursoremailCur= cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
            projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
            newString[]{email}, null);
    if (emailCur.moveToNext()) {
        intcolumnIndex= emailCur.getColumnIndex(
                              ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
        longcontactId= emailCur.getLong(columnIndex);

        u = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 
                                       contactId);
        u = Uri.withAppendedPath(u, 
                                ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }
    return u;
}

Alternatively, you can get the photo stream utilizing method ContactsContract.Contacts .openContactPhotoInputStream(ContentResolver, Uri) this way:

private InputStream getPhotoInputStreamFromEmail(String email) {
    Uriu=null;
    String[] projection = { ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String photoUri;
    ContentResolvercr= getContentResolver();
    CursoremailCur= cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
            newString[]{email}, null);
    if (emailCur.moveToNext()) {
        intcolumnIndex= emailCur.getColumnIndex(
                ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
        longcontactId= emailCur.getLong(columnIndex);

        u = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                contactId);
        return ContactsContract.Contacts.openContactPhotoInputStream(cr, u);
    }
    returnnull;
}

Post a Comment for "How Do You Get A Contact Photo Through The Contact Provider In Android 2.3.6?"