gpt4 book ai didi

java - 从 Web 服务加载数据后刷新时清除 picasso 图像缓存

转载 作者:行者123 更新时间:2023-12-01 21:17:21 25 4
gpt4 key购买 nike

使用 SwipeRefreshLayout 刷新时,我执行了以下代码。这应该加载新的个人资料图片,但旧图像似乎以某种方式缓存在设备上。当我退出应用程序并返回 Activity 时,新的个人资料图片可用。但刷新并不能解决这个问题。我在这里能做什么?

滑动:

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,因为我是从互联网下载图像的。但这也许不是我应该做的?

最佳答案

这是我的代码,如果该文件的缓存存在,它将首先从缓存加载图像,然后更新缓存文件。如果不存在,则从互联网(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)));
}
});

关于java - 从 Web 服务加载数据后刷新时清除 picasso 图像缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com