现在我正在使用以下代码检索图像并且它工作正常。但我想使用universal-image-loader
实现缓存。我已经在我的其他项目中实现了它,其中我有完整的URL图像像〜\ images \ pic1.jpeg。另一方面,在使用Contacts api v3时,我必须处理输入流,而且我没有这样完整的url.so我不知道如何实现universal-image-loader
。
供参考:Contact api v3
这是我现在正在使用的代码:
Bitmap bm=BitmapFactory.decodeResource(HomeActivity.this.getResources(),
R.drawable.profile_pic);
CONSTANTS.buffer = new byte[4096];
// iterate the loop upto number of contacts
for(int i=0;i<CONSTANTS.contactArrayList.size();i++)
{
//if the contact has any profile pic then retrieve it otherwise set default profile pic from drawable folder
if(CONSTANTS.contactArrayList.get(i).getContactPhotoLink().getEtag()!=null)
{
try
{
GDataRequest request = CONSTANTS.mContactService.createLinkQueryRequest(CONSTANTS.contactArrayList.get(i).getContactPhotoLink());
request.execute();
InputStream in = request.getResponseStream();
CONSTANTS.buffer = ByteStreams.toByteArray(in);
bm = BitmapFactory.decodeByteArray(CONSTANTS.buffer, 0, CONSTANTS.buffer.length);
in.close();
request.end();
}
catch (Exception e) {
UTILS.Log_e("loadProfilePics error", e.toString());
}
}
else
{
bm = BitmapFactory.decodeResource(HomeActivity.this.getResources(),
R.drawable.profile_pic);
}
CONSTANTS.contactArrayList.get(i).setContactPhoto(bm);
}
答案 0 :(得分:1)
是的,universal-image-loader
允许您这样做。只需按照以下步骤操作:
contacts-api-v3://user_id=<user_id>
提供一种方法来检索此类网址的InputStream
:
public class CustomImageDownloader extends URLConnectionImageDownloader {
@Override
protected InputStream getStreamFromOtherSource(URI imageUri) throws IOException {
if (imageUri.getScheme().equals("contacts-api-v3")) {
// here you can use code provided in your question
return retriveInputStreamForThisUser();
}
return null;
}
}
配置ImageLoader
以使用您的CustomImageDownloader
:
final ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context);
// some basic configuration should be here
builder.imageDownloader(new CustomImageDownloader());
现在您可以这样使用它:
ImageLoader.getInstance().displayImage("contacts-api-v3://user_id=123", imageView);