在我的应用程序中,我想从url下载图片并将它们显示在recyclerView中。基本上一切都很好-当我下载图片,关闭wifi,移动数据缓存的图片就会显示出来。尝试了几次-它工作得很好。然而..。例如,3-4小时后,我再次尝试在脱机模式下启动应用程序,但图像没有显示。知道我做错了什么吗?下面是我在基本活动(onCreate)中的代码:
Picasso.Builder builder = new Picasso.Builder(getContext());
builder.downloader(new OkHttp3Downloader(getContext(),Integer.MAX_VALUE));
Picasso built = builder.build();
built.setIndicatorsEnabled(true);
built.setLoggingEnabled(true);然后我下载并缓存图像,如下所示:
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;
}发布于 2018-03-19 08:34:58
您好,您可以试试这个:
Picasso.with(mContext)
.load(att.get(position).url)
.noFade()
.resize(150,150)
.centerCrop()
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(iv);发布于 2018-03-19 16:47:14
我对毕加索知之甚少。我有一个建议,你必须使用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;
}它有自动缓存配置,这意味着我们不需要为cache.It在第一次使用后完美加载的图像而烦恼。
在Glide V4中
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.
}
});发布于 2020-06-19 18:50:34
如果您参考最新版本的毕加索(2.71828)的文档,并了解毕加索如何在内部处理它,您就会理解为什么您的图像不能从磁盘/缓存加载。
解释得很好的答案在这里,https://stackoverflow.com/a/57114949/1508631。
https://stackoverflow.com/questions/49352482
复制相似问题