在我遇到这个路障之前,用毕加索( Picasso )加载图像似乎非常容易。不知道为什么!如果联系人只有缩略图,或者,如果我专门要求PHOTO_THUMBNAIL_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);
}
}值得注意的是:如果我使用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);
}
}这将加载高分辨率照片,如果有,如果没有,则显示低分辨率照片,如果没有为联系人设置照片,则设置为空/空。
发布于 2014-01-19 08:10:15
你试过使用contact uri吗?
openContactPhotoInputStream中的最后一个布尔参数将在可用的情况下为您提供高分辨率的照片。
不要使用photo uri,而是使用contact uri或contact lookup uri。
更新的问题已经回答了,我想在这里发布相关细节:这里发布了一个小型测试应用程序(您需要Android ):https://github.com/copolii/PicassoContactsTest
如果同时设置了placeholder和error图标,则会为没有图片的联系人显示error图标。我建议把社交脸的家伙设为你的位置持有者,没有错误图标。这样,如果联系人没有照片,你的位置持有者就会继续工作。
如果do想要区分这两者,请选择您的错误图标,记住上面的内容(即不要使用大的红色OMFG错误指示器)。
-以前的内容-
如果这有帮助的话请告诉我。
我做了联系人照片加载的工作,除非遗漏了什么,否则您应该自动获得高分辨率图片(API 14+):
if (SDK_INT < ICE_CREAM_SANDWICH) {
return openContactPhotoInputStream(contentResolver, uri);
} else {
return openContactPhotoInputStream(contentResolver, uri, true);
}看来openContactPhotoInputStream不喜欢PHOTO_URI。
安卓文档: openContactPhotoInputStream
如果URI是可区分的,我也可以轻松地添加对PHOTO_URI的支持(不过,我必须先了解如何加载它)。我已经在确定给定的uri是联系人照片uri还是联系人查找uri (较早的android版本不喜欢将查找uri输入到openContactPhotoInputStream中,因此在将查找uri传递给openContactPhotoInputStream之前,我必须将查找uri取消到联系人uri中)。
我希望这能帮到你。
https://stackoverflow.com/questions/21157630
复制相似问题