lisovaccaro lisovaccaro - 9 days ago 6
Android Question

Simple way to add contact photo to ImageView?

I'm having problems getting and setting a contact's image as a view's background, surprisingly there are few examples on how to do it. I'm trying to build something similar to the People app which displays big contact photos.

This is what I'm doing right now:

Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
Bitmap bm = BitmapFactory.decodeStream(input);
Drawable d = new BitmapDrawable(bm);
button.setBackgroundDrawable(drawable);


This works however the URI it uses gets a thumbnail picture, so even if there is a big photo images looks very bad when scaled to fit the imageView. I know another method to get the URI that actually gets a big photo which is:

final Uri imageUri = Uri.parse(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)));


However I haven't managed to get it to the imageView, maybe the code above can be adapted to use the second uri. If you know how to use the second uri or if there is an easier way to get the contact image than through the URI please tell me. Any info will be thanked.

Answer

Good job in getting the URI. You're almost there. First of all consider using PHOTO_THUMBNAIL_URI instead of PHOTO_URI, as it may be what you need in terms of size.

Edit : FYI, PHOTO_THUMBNAIL_URI is available starting API 11. You can still use it conditionally.

If you want to use an external library, 'Android Universal Image Loader' is definitely what you are looking for, as starting from it's 1.7.1 version from a few days ago, it added support for content schemes and it is pretty smart, memory wise. It also has a lot of customization options.

Edit: this lib is already dead. Use Fresco instead.

If you'd rather be nicer to your final bundle size and write the code yourself,

You need to get and decode the input stream of that content; This should be done on a background thread. Check out this connivence method; You initialise it with your image view and the uri you got and start it when you want to load the ImageView.

private class ContactThumbnailTask extends AsyncTask<Void, Void, Bitmap> {

    private WeakReference<ImageView> imageViewWeakReference;
    private Uri uri;
    private String path;
    private Context context;


    public ContactThumbnailTask(ImageView imageView, Uri uri, Context context) {
        this.uri = uri;
        this.imageViewWeakReference = new WeakReference<ImageView>(imageView);
        this.path = (String)imageViewWeakReference.get().getTag(); // to make sure we don't put the wrong image on callback
        this.context = context;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        InputStream is = null;
        try {
            is = context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Bitmap image = null;
        if (null!= is)
            image=  BitmapFactory.decodeStream(is);

        return image;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewWeakReference != null && imageViewWeakReference.get() != null && ((String)imageViewWeakReference.get().getTag()).equals(path) && null != bitmap)
            imageViewWeakReference.get().setImageBitmap(bitmap);
    }
}