[英]Clear Picasso image cache on refresh after loading data from web service
在使用SwipeRefreshLayout刷新时,我执行了以下代码。 这应加载新的个人资料图片,但似乎旧图像以某种方式缓存在设备上。 当我退出应用程序并返回活动时,可以使用新的个人资料图片。 但是刷新不会解决这个问题。 我在这里可以做什么?
滑动:
private void setSwipeRefreshLayout(boolean isOnline, String userId) {
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setOnRefreshListener(() -> {
if (!isOnline) {
mSwipeRefreshLayout.setRefreshing(false);
} else {
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
if (userId != null && userId.equals(ParseUser.getCurrentUser().getObjectId())) {
createProfileHeader(userId);
Picasso.with(getApplicationContext()).invalidate(imagePath);
}
}
});
}
从Parse检索个人资料照片:
if (headerUserObject.getParseFile("profilePicture") != null) {
Picasso.with(getApplicationContext())
.load(headerUserObject.getParseFile("profilePicture").getUrl())
.placeholder(R.color.placeholderblue)
.into(((ImageView) findViewById(R.id.profile_picture)));
fadeInProfilePicture();
}
我不确定如何从上面获取imagePath
,因为我是从互联网上下载图像的。 但这也许不是我应该做的吗?
这是我的代码,如果该文件的缓存存在,它将首先从缓存中加载图像,然后更新缓存文件。 如果不存在,请从Internet(url)加载它。
Picasso.with(getApplicationContext()).load(headerUserObject.getParseFile("profilePicture").getUrl())
.placeholder(R.color.placeholderblue).networkPolicy(NetworkPolicy.OFFLINE) // try to load the image from cache first
.into(((ImageView) findViewById(R.id.profile_picture)), new Callback() {
@Override
public void onSuccess() {
//cache file for this url exists, now update the cache for this url
if(networkAvaialable()) // if internet is working update the cache file
Picasso.with(getApplicationContext()).invalidate(headerUserObject.getParseFile("profilePicture").getUrl());
}
@Override
public void onError() { // if cache file does not exist load it from the internet
Picasso.with(getApplicationContext()).load(headerUserObject.getParseFile("profilePicture").getUrl())
.placeholder(R.color.placeholderblue)
.into(((ImageView) findViewById(R.id.profile_picture)));
}
});
试试这个
Picasso.memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.