6

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.

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • I think this is what you are looking for http://stackoverflow.com/a/8586182/1832000 – Cristian Holdunu Jan 29 '13 at 00:45
  • I haven't been able to implement it. It needs variables url and file `ImageOperations(this,url,filename)` I don't know what these are. If you know what to use as url and filename if it works I will accept your answer. – lisovaccaro Jan 29 '13 at 01:51

6 Answers6

8

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);
    }
}
Ben Max Rubinstein
  • 1,803
  • 1
  • 13
  • 15
  • I was using an ImageButton before. I noticed that changing to ImageView and using setImageUri() actually sets the image fine. However there is a small problem, I'm not sure why but I'm getting a blank space on the top and on the bottom of the ImageView. If you know how to fix it I'll accept the answer – lisovaccaro Feb 03 '13 at 00:51
  • I couldn't implement this btw I get blank spaces in all my imageviews – lisovaccaro Feb 03 '13 at 00:51
  • Is there any problem with using setImageUri? Would using a backgrond thread be more efficient – lisovaccaro Feb 03 '13 at 00:52
  • setImageUri does decoding on the UI thread and that causes a latency hiccup, so I recommend against it. Have you looked into the scaletype of the ImageView? Are the sizes fixed? – Ben Max Rubinstein Feb 03 '13 at 07:43
2

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.

Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32
0

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

madlymad
  • 6,367
  • 6
  • 37
  • 68
0

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); 
daniel_c05
  • 11,438
  • 17
  • 60
  • 78
0

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.

Chanaka udaya
  • 5,134
  • 4
  • 27
  • 34
0

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);
polis
  • 737
  • 7
  • 6