I am trying to use Glide to load an image from the server. But the problem is dynamic URL and every time I open the page, it load the same image from server
this is my code
@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);
}
Finally, figure it out by changing cache key
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;
}
}