【发布时间】:2018-08-27 09:27:46
【问题描述】:
在我的应用程序中,我想从 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;
}
【问题讨论】: