Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having scanned the many questions and answers to the problem of displaying a thumbnail I thought I would post my solution to this particular conundrum as I could only find a couple that worked at all and none that provided a good canned solution for the lazy developer. </p> <p>The class below takes a Context, QuickContactBadge and a telephone number and will attach a locally stored image to the badge if there is one available for the specified phone number.</p> <p>Here's the class:</p> <pre><code>public final class QuickContactHelper { private static final String[] PHOTO_ID_PROJECTION = new String[] { ContactsContract.Contacts.PHOTO_ID }; private static final String[] PHOTO_BITMAP_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO }; private final QuickContactBadge badge; private final String phoneNumber; private final ContentResolver contentResolver; public QuickContactHelper(final Context context, final QuickContactBadge badge, final String phoneNumber) { this.badge = badge; this.phoneNumber = phoneNumber; contentResolver = context.getContentResolver(); } public void addThumbnail() { final Integer thumbnailId = fetchThumbnailId(); if (thumbnailId != null) { final Bitmap thumbnail = fetchThumbnail(thumbnailId); if (thumbnail != null) { badge.setImageBitmap(thumbnail); } } } private Integer fetchThumbnailId() { final Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); final Cursor cursor = contentResolver.query(uri, PHOTO_ID_PROJECTION, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC"); try { Integer thumbnailId = null; if (cursor.moveToFirst()) { thumbnailId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); } return thumbnailId; } finally { cursor.close(); } } final Bitmap fetchThumbnail(final int thumbnailId) { final Uri uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, thumbnailId); final Cursor cursor = contentResolver.query(uri, PHOTO_BITMAP_PROJECTION, null, null, null); try { Bitmap thumbnail = null; if (cursor.moveToFirst()) { final byte[] thumbnailBytes = cursor.getBlob(0); if (thumbnailBytes != null) { thumbnail = BitmapFactory.decodeByteArray(thumbnailBytes, 0, thumbnailBytes.length); } } return thumbnail; } finally { cursor.close(); } } } </code></pre> <p>And here's a typical use case inside an activity:</p> <pre><code>String phoneNumber = "..."; QuickContactBadge badge = (QuickContactBadge) view.findViewById(R.id.friend); new QuickContactHelper(this, badge, phoneNumber).addThumbnail(); </code></pre> <p>In a fragment it will be slightly different:</p> <pre><code>String phoneNumber = "..."; QuickContactBadge badge = (QuickContactBadge) view.findViewById(R.id.friend); new QuickContactHelper(getActivity(), badge, phoneNumber).addThumbnail(); </code></pre> <p>Now there are ways to be more efficient - for example if you are rendering a message timeline you'd want to re-use the same bitmap object for every badge instance for a given phone number instead of constantly creating new helper class instances and re-retrieving the bitmap - but my purpose here was to post a solution that is stripped down to the absolute minimum for clarity whilst at the same time providing a complete and usable solution out of the box. This solution has been built and tested on Andriod 4.0, and tested on 4.1 as well.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload