简体   繁体   English

毕加索会加载PHOTO_THUMBNAIL_URI,但不会加载PHOTO_URI

[英]Picasso loads PHOTO_THUMBNAIL_URI but not PHOTO_URI

Loading images with Picasso is seemingly so easy, until I hit this roadblock. 在我遇到这个障碍之前,用Picasso加载图像似乎很容易。 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. 如果联系人仅具有缩略图,或者我特别要求PHOTO_THUMBNAIL_URI,我可以通过PHOTO_URI从联系人中加载照片。

    @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); 对于它的价值:如果我使用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)); 如果我回到使用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: 更新:感谢@copolii及其下面的回答,以下内容现在可以完美地与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. 这会加载高分辨率的照片,如果有一张,则显示低分辨率的照片,如果没有为联系人设置照片,则将其设置为空白/ null。

Have you tried using a contact uri ? 您是否尝试过使用contact uri

That last boolean parameter in openContactPhotoInputStream promises to get you the high res photo if one is available. openContactPhotoInputStream最后一个布尔参数承诺可以为您提供高分辨率的照片(如果有)。

Instead of using a photo uri use a contact uri or a contact lookup uri . 代替使用photo uri使用contact uricontact 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 更新由于已经回答了问题,尽管我会在此处发布相关详细信息:在此处发布了一个小型测试应用程序(您需要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. 如果同时设置了placeholdererror图标,则没有图片的联系人将显示error图标。 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 (ie don't use a big red OMFG error indicator). 如果想在两者之间进行区分,选择与考虑到上述的错误图标(即不使用大红色OMFG错误指示)。

--- 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: 我已经完成了联系人照片的加载工作,除非丢失了某些东西,否则您应该自动获得高分辨率图片(API 14+):

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. 似乎openContactPhotoInputStream不喜欢PHOTO_URI。

Android Docs: openContactPhotoInputStream Android文档: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). 如果URI是可区分的,那么我也可以轻松地添加对PHOTO_URI的支持(尽管我必须先找出如何加载它)。 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 ). 我已经在确定给定的uri是联系人照片uri还是联系人查找uri (较早的android版本不喜欢将查找uris馈入openContactPhotoInputStream因此我必须openContactPhotoInputStream 查找uri引用到联系人uri中,然后再将其传递给openContactPhotoInputStream )。 。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM