2017-05-27 80 views
0

我正嘗試使用Glide從服務器加載圖像。但問題是動態的URL我每次打開頁面時,它從服務器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

最後弄明白改變緩存鍵

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; 
} 

}