我正在尝试使用Glide从服务器加载图像。但问题是动态URL,每次打开页面时,它都会从服务器加载相同的图像
这是我的代码
@android.databinding.BindingAdapter("imageurl")
public static void loadImageFromUrl(ImageView imageView, String url) {
if (url == null || url.isEmpty()) return;
GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
.addHeader("Authorization", "Bearer " + NetworkConstant.TOKEN).build());
View parent = imageView.getRootView();
final ProgressBar progressBar = (ProgressBar) parent.findViewById(R.id.progressBar);
Glide.with(imageView.getContext()).load(glideUrl).listener(new RequestListener<GlideUrl, GlideDrawable>() {
@Override
public boolean onException(Exception e, GlideUrl model, Target<GlideDrawable> target, boolean isFirstResource) {
Log.e("Error", e.getMessage());
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, GlideUrl model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.e("IsFromCache", isFromMemoryCache + " : firstResource" + isFirstResource);
if (progressBar != null && progressBar.getVisibility() == View.VISIBLE) {
progressBar.setVisibility(View.GONE);
}
return false;
}
})
.fitCenter()
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
}
答案 0 :(得分:1)
最后,通过更改缓存键
来计算出来public class GlideNoTokenUrl extends GlideUrl {
public GlideNoTokenUrl(URL url) {
super(url);
}
public GlideNoTokenUrl(String url) {
super(url);
}
public GlideNoTokenUrl(URL url, Headers headers) {
super(url, headers);
}
public GlideNoTokenUrl(String url, Headers headers) {
super(url, headers);
}
@Override
public String getCacheKey() {
String url = toStringUrl();
if (url.contains("?")) {
url = url.substring(0, url.indexOf("?"));
}
return url;
}
}