lisovaccaro
lisovaccaro

Reputation: 33996

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.

Upvotes: 6

Views: 5345

Answers (6)

Ben Max Rubinstein
Ben Max Rubinstein

Reputation: 1813

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);
    }
}

Upvotes: 8

polis
polis

Reputation: 933

To be able to do that you have to just add one last parameter preferHighres = true:

openContactPhotoInputStream (ContentResolver cr, Uri contactUri, boolean preferHighres)

If preferHighres is true and the contact has a higher resolution photo available, it is returned. If false, this function always tries to get the thumbnail

     Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id));
     InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri, true);

All the images probably would have differnet sizes. In order to resize them I use next code:

    Bitmap bm = BitmapFactory.decodeStream(input);
    bm = Bitmap.createScaledBitmap(photo, contactImageWidth, contactImageheight, false);
    Drawable d = new BitmapDrawable(getContext(), bm);
    button.setBackgroundDrawable(d);

Upvotes: 0

Chanaka udaya
Chanaka udaya

Reputation: 5326

You can easily set the contact photo to the image view by using the following method.

public String getImageUriString(String phoneNumber)
{
    ContentResolver resolver = context.getContentResolver();
    Cursor names = resolver.query(
            Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)),
            null, null, null, null);

    names.moveToFirst();
    String name = "";
    if(!names.isAfterLast())
    {
        name = names.getString(names.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));

    }
    else
    {
        name = null;
    }

    names.close();

    return name;
}





public void setImageView(ImageView contactPhoto) {

String photoUriString = di.getImageUriString(contactNumber);
        if(photoUriString != null) {
            Uri photoUri = Uri.parse(photoUriString);
            contactPhoto.setImageURI(photoUri);
        } else {
            contactPhoto.setImageResource(R.drawable.icon_contact);
        }
}

From your class, set the image view with the uri getting from the above method.

Upvotes: 0

Thirumalvalavan
Thirumalvalavan

Reputation: 2768

It is suggestion. First know one thing.

When you set a contacts image. First Android show the Cropping activity for that image. Like enter image description here

****carefully see above image. Android crop the image as square shape. And store the square shape image as a Blob in contacts.(It is not individual image. It is blob.)
You get a square shape image for your image view from your coding. so top and bottom show black color only. Because your mobile in rectangle shape.****

If you want to show full screen image. Please set a big image to contacts through programmatically. Lot of examples available in internet.

All the best for your try. If you have any doubts. Please provide comments.

Upvotes: 2

daniel_c05
daniel_c05

Reputation: 11528

Use an external library to do that for you. Or browse the code and make something similar your own way.

Here's one I use on several of my own apps: UrlImageViewHelper

The code would go like this:

Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id));  
UrlImageViewHelper.setUrlDrawable(button, uri.toString(), R.drawable.dummy_contact_photo); 

Upvotes: 0

madlymad
madlymad

Reputation: 6530

You can try using SmartImageView: http://loopj.com/android-smart-image-view/ extends imageview and also loads images asynchronously.

Upvotes: 0

Related Questions