How to get Android Contact thumbnail
Asked Answered
J

2

11

I have a listview adapter and I'm trying the following in the newView method:

@Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        long contactId = Long.valueOf(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))); 
        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        boolean hasPhone = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
        String thumbnailUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)); 

        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(contactName);
        }

        name_text.setTag(new Contact(contactId, hasPhone));

        ImageView thumbnail = (ImageView) v.findViewById(R.id.thumbnail);
        if (thumbnailUri != null) {
            thumbnail.setImageURI(Uri.parse(thumbnailUri));
        } else {
            thumbnail.setImageResource(R.drawable.ic_launcher);
        }

        return v;
    }

But when I try to parse the Uri that is stored in thumbnailUri, I receive the following error:

   08-09 01:58:38.619: I/System.out(1471): resolveUri failed on bad bitmap uri: content://com.android.contacts/contacts/1/photo

Am I going about this the wrong way? Any help would be greatly appreciated!

Juniorjuniority answered 8/8, 2012 at 14:1 Comment(3)
Can you put a logcat info in there to see what the URI that you're trying to resolve is and post it here? Log.i("URI", thumbnailUri);Fong
content://com.android.contacts/contacts/1/photo, which I believe is correct. Maybe it's just because I'm using an emulator. I need to get a real device :/Juniorjuniority
Just got myself a real device. This works, my code is fine.Juniorjuniority
I
12
private Uri getPhotoUriFromID(String id) {
    try {
        Cursor cur = getContentResolver()
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + id
                                + " AND "
                                + ContactsContract.Data.MIMETYPE
                                + "='"
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                + "'", null, null);
        if (cur != null) {
            if (!cur.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
    return Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

This is the function where you need to pass contact id and you will get the URI of the image which you can set easily in the imageview.

use the response uri of this function like imageView.setImageURI(uri)

Hope it will work for your code.

Implausibility answered 8/8, 2012 at 14:21 Comment(6)
Tried this, and it returns the same Uri as the one I was already getting (content://com.android.contacts/contacts/1/photo) unfortunately. I'm still receiving the same error message.Juniorjuniority
there is two id of contact is there. one is contact id and other is raw contact id so which id are you supply to function?it might be problem with id and so its not getting the Uri.Implausibility
Also tried this: String stringUri = imageUri.getPath(); //works String stringUri = imageUri.toString(); //does not work i.setImageURI(Uri.parse(stringUri)); it may be works for you. because in my code function works correctly.Implausibility
Turns out it was just to do with using an emulator. Sorry about that, thanks for your help though. Would have kept assuming my code was wrong if not for your help :)Juniorjuniority
this method returns a small image instead of a large one. How i can retrieve a large image instead of a small one?Lardon
@GeorgePanayi You'll need to grab the DISPLAY_PHOTO instead. See the official docs here: developer.android.com/reference/android/provider/…Atp
H
0

Probably will help someone. Just leave it here.
This way you can get thumbnail uri by contact id.
Tested on Android API 28.

    ContentResolver cr = getContentResolver();
    String[] projection = new String[] {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
    String where = ContactsContract.Contacts.NAME_RAW_CONTACT_ID = "?";
    String[] selectionArgs = {your_contact_id}
    Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI, projection, where, selectionArgs, null);

    String thumbnailUri;
    if ((cur != null ? cur.getCount() : 0) > 0) {
        if (cur.moveToNext()) {
            thumbnailUri = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
        }
    }
    if(cur!=null){
        cur.close();
    }
    return thumbnailUri;
Hymanhymen answered 23/11, 2020 at 14:44 Comment(1)
how we can load this URI in jetpack compose using Coil?Waltz

© 2022 - 2024 — McMap. All rights reserved.