javaandroidandroid-contactspicasso

Picasso loads PHOTO_THUMBNAIL_URI but not PHOTO_URI


Loading images with Picasso is seemingly so easy, until I hit this roadblock. Not sure why! I can load photos from contacts via PHOTO_URI if the contacts only have a thumbnail, or, if I instead ask for PHOTO_THUMBNAIL_URI specifically.

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        
        ImageView icon = (ImageView)view.findViewById(R.id.ContactImage);
        String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));

        if (photoUri == null) {
            icon.setImageDrawable(null);
        } else {
            Picasso.with(context).load(photoUri).into(icon);
        }
    }

For what it's worth: if I use Picasso.with(context).load(photoUri).placeholder(R.drawable.placeholder).error(R.drawable.error).into(icon); then I see the placeholder image in the place of every contact who has a high res image. I never see an "error" picture. If I revert back to just using icon.setImageURI(Uri.parse(photoUri)); then I see the high res contact images again just fine. (But then I don't have a snazzy async caching picture loader!)


Solution

  • Have you tried using a contact uri?

    That last boolean parameter in openContactPhotoInputStream promises to get you the high res photo if one is available.

    Instead of using a photo uri use a contact uri or a contact lookup uri.

    UPDATE Since the question has been answered, I though I'd post the relevant details here: A small test app is posted here (You need Android Studio): https://github.com/copolii/PicassoContactsTest

    If you set both a placeholder and an error icon, the error icon is displayed for contacts who do not have a picture. I'd recommend setting the social face guy as your place-holder and no error icon. That way, your place-holder stays on if the contact has no picture.

    If you do want to differentiate between the two, choose your error icon with the above in mind (i.e. don't use a big red error indicator).


    --- Previous Content ---

    I did the work for the contacts photo loading and unless I'm missing something, you should get the high resolution picture (API 14+) automatically:

    if (SDK_INT < ICE_CREAM_SANDWICH) {
      return openContactPhotoInputStream(contentResolver, uri);
    } else {
      return openContactPhotoInputStream(contentResolver, uri, true);
    }
    

    It seems that the openContactPhotoInputStream doesn't like the PHOTO_URI.

    Android Docs: openContactPhotoInputStream

    If the URIs are distinguishable I can easily add support for PHOTO_URI as well (I have to find out how to load it first though). I'm already determining if the given uri is a contact photo uri or a contact lookup uri (older android versions do not like lookup uris being fed into openContactPhotoInputStream so I have to dereference the lookup uri into a contact uri before passing it to openContactPhotoInputStream).