1

用毕加索加载图像似乎很容易,直到我遇到了这个障碍。不知道为什么!如果联系人只有缩略图,或者我专门要求 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);
        }
    }

对于它的价值:如果我使用Picasso.with(context).load(photoUri).placeholder(R.drawable.placeholder).error(R.drawable.error).into(icon);,那么我会在每个具有高分辨率图像的联系人的位置看到占位符图像。我从未见过“错误”图片。如果我恢复为仅使用,icon.setImageURI(Uri.parse(photoUri));那么我再次看到高分辨率联系人图像就好了。(但是我没有时髦的异步缓存图片加载器!)

更新:感谢@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);
    }

}

这将加载高分辨率照片,如果有,则显示低分辨率照片,如果没有为联系人设置照片,则将其设置为空白/空。

4

1 回答 1

4

您是否尝试过使用contact uri?

如果有可用的高分辨率照片,那么最后一个布尔参数openContactPhotoInputStream承诺会给您提供高分辨率照片。

而不是photo uri使用 acontact uri或 a contact lookup uri

更新 由于问题已得到解答,我虽然会在此处发布相关详细信息:此处发布了一个小型测试应用程序(您需要 Android Studio):https ://github.com/copolii/PicassoContactsTest

如果同时设置 aplaceholdererror图标,error则为没有图片的联系人显示图标。我建议将社交面孔人设置为您的占位符,并且没有错误图标。这样,如果联系人没有图片,您的占位符就会保持打开状态。

如果您确实想区分这两者,请根据上述情况选择错误图标(即不要使用大红色的 OMFG错误指示器)。

--- 以前的内容 ---

让我知道这是否有帮助。

我完成了联系人照片加载的工作,除非我遗漏了什么,否则您应该自动获得高分辨率图片(API 14+):

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

似乎 openContactPhotoInputStream 不喜欢 PHOTO_URI。

Android 文档:openContactPhotoInputStream

如果 URI 是可区分的,我也可以轻松添加对 PHOTO_URI 的支持(不过我必须先找出如何加载它)。我已经在确定给定的 uri 是联系人照片 uri还是联系人查找 uri(较旧的 android 版本不喜欢输入查找 uri openContactPhotoInputStream,因此我必须在将查找 uri传递给联系人 uri之前将其取消引用openContactPhotoInputStream)。

我希望这有帮助。

于 2014-01-19T08:10:15.777 回答