Pergunta

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!)

UPDATE: Thanks to @copolii and his answers below, the following now works flawlessly with Picasso 2.1.1:

@Override
public void bindView(View view, Context context, Cursor cursor) {

    Long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));

    ImageView icon = (ImageView)view.findViewById(R.id.ContactImage);

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

}

This loads the higher-res photo, if there is one, and if not, shows the low-res photo, and if there is no photo set for a contact, it's set to a blank / null.

Foi útil?

Solução

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 OMFG error indicator).

--- Previous Content ---

Let me know if that helps.

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).

I hope this helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top