Android 安卓毕加索缓存数据
在我的应用程序中,我想从url下载图像并在recyclerView中显示它们。基本上一切都正常-当我下载图像时,关闭wifi和移动数据-缓存的图像正在显示。试了好几次,效果很好。然而。。。例如,3-4小时后,我再次尝试以脱机模式启动应用程序,但没有显示图像。知道我做错了什么吗?以下是我在基本活动(onCreate)中的代码: 然后我下载并缓存如下图像:Android 安卓毕加索缓存数据,android,caching,picasso,Android,Caching,Picasso,在我的应用程序中,我想从url下载图像并在recyclerView中显示它们。基本上一切都正常-当我下载图像时,关闭wifi和移动数据-缓存的图像正在显示。试了好几次,效果很好。然而。。。例如,3-4小时后,我再次尝试以脱机模式启动应用程序,但没有显示图像。知道我做错了什么吗?以下是我在基本活动(onCreate)中的代码: 然后我下载并缓存如下图像: public void onResponse(Call<CallbackListPost> call, Response<Ca
public void onResponse(Call<CallbackListPost> call, Response<CallbackListPost> response) {
CallbackListPost resp = response.body();
if (resp != null && resp.status.equals("ok")) {
post_total = resp.count_total;
displayApiResult(resp.posts);
controller = new RealmController(getActivity().getApplication());
controller.savePost(resp.posts);
for(Post post : resp.posts) {
for (final Attachment att : post.attachments) {
Picasso.with(getActivity())
.load(att.url)
.fetch(new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
Log.e(TAG, "onSuccess: " + att.url );
}
@Override
public void onError() {
}
});
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv;
if(convertView == null) {
iv = new ImageView(mContext);
} else {
iv = (ImageView) convertView;
}
Picasso.with(mContext)
.load(att.get(position).url)
.noFade()
.resize(150,150)
.centerCrop()
.into(iv);
return iv;
}
您好,您可以试试这个:
Picasso.with(mContext)
.load(att.get(position).url)
.noFade()
.resize(150,150)
.centerCrop()
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(iv);
我对毕加索了解不多。 我建议您必须使用Glide,一种图像缓存工具。 它的图书馆很小,速度也很快 Glide生成的API的简单用例如下所示: //对于简单视图:
@Override public void onCreate(Bundle savedInstanceState) {
...
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
GlideApp.with(this).load("goo.gl/gEgYUd").into(imageView);
}
// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
final ImageView myImageView;
if (recycled == null) {
myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
} else {
myImageView = (ImageView) recycled;
}
String url = myUrls.get(position);
GlideApp
.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
return myImageView;
}
它有自动缓存配置,这意味着我们不需要担心图像缓存。第一次使用后,它可以完美地加载
在Glidev4中
Glide.with(上下文)
.load(“/user/profile/photo/path”)
.asBitmap()
.toBytes()
.centerCrop()
.into(新的SimpleTarget(250250){
@凌驾
public void onResourceReady(字节[]数据,动画动画){
//将您的字节发布到后台线程并上传到此处。
}
});
如果您参考最新版本毕加索(2.71828)的文档,了解毕加索如何在内部处理它,您就会明白为什么您的图像没有从磁盘/缓存加载
答案就在这里
@Override public void onCreate(Bundle savedInstanceState) {
...
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
GlideApp.with(this).load("goo.gl/gEgYUd").into(imageView);
}
// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
final ImageView myImageView;
if (recycled == null) {
myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
} else {
myImageView = (ImageView) recycled;
}
String url = myUrls.get(position);
GlideApp
.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
return myImageView;
}
Glide.with(context)
.load(“/user/profile/photo/path”)
.asBitmap()
.toBytes()
.centerCrop()
.into(new SimpleTarget<byte[]>(250, 250) {
@Override
public void onResourceReady(byte[] data, GlideAnimation anim) {
// Post your bytes to a background thread and upload them here.
}
});