用 Picasso 加载图像看起来很容易,直到我遇到了这个障碍。不知道为什么!如果联系人只有缩略图,我可以通过 PHOTO_URI 从联系人加载照片,或者,如果我专门要求 PHOTO_THUMBNAIL_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);
}
}
它的值(value):如果我使用 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);
}
}
这将加载高分辨率照片,如果有,则显示低分辨率照片,如果没有为联系人设置照片,则将其设置为空白/空。
最佳答案
您是否尝试过使用 contact uri
?
openContactPhotoInputStream
中的最后一个 boolean 参数 promise 为您提供高分辨率照片(如果有的话)。
不要使用照片 uri
,而是使用联系人 uri
或联系人查找 uri
。
更新 由于问题已得到解答,我想在这里发布相关详细信息: 此处发布了一个小型测试应用程序(您需要 Android Studio):https://github.com/copolii/PicassoContactsTest
如果您同时设置了placeholder
和error
图标,则error
图标会为没有图片的联系人显示。我建议将社交面孔人物 设置为您的占位符并且没有错误图标。这样,如果联系人没有图片,您的占位符将保持打开状态。
如果您确实想要区分这两者,请在选择错误图标时牢记上述内容(即不要使用大红色 OMFG 错误指示器)。
--- 以前的内容 ---
如果有帮助,请告诉我。
我完成了加载联系人照片的工作,除非我遗漏了什么,否则您应该会自动获得高分辨率图片 (API 14+):
if (SDK_INT < ICE_CREAM_SANDWICH) {
return openContactPhotoInputStream(contentResolver, uri);
} else {
return openContactPhotoInputStream(contentResolver, uri, true);
}
openContactPhotoInputStream 似乎不喜欢 PHOTO_URI。
Android Docs: openContactPhotoInputStream
如果 URI 是可区分的,我也可以轻松地添加对 PHOTO_URI 的支持(尽管我必须首先找出如何加载它)。我已经在确定给定的 uri 是联系人照片 uri 还是联系人查找 uri(较旧的 android 版本不喜欢查找 uris 被送入 openContactPhotoInputStream
所以我必须将 lookup uri 取消引用到 contact uri 中,然后再将其传递给 openContactPhotoInputStream
)。
希望对您有所帮助。
关于java - Picasso 加载 PHOTO_THUMBNAIL_URI 但不加载 PHOTO_URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21157630/