2016-11-11 45 views
1

我正在从Android S3加载图像的Android应用程序中工作。 图片URL随令牌和有效期密钥随机更改。出于这个原因,我无法缓存图像滑翔。Glide图像缓存ID不是url

有什么方法可以将Glide缓存键设置为任何静态ID(如图像ID)而不是URL。

我重视我的代码片段从AWS

加载图像
Glide.with(remoteGalleryAct).load(photoFinalImageURL) 
       .signature(new StringSignature(getImageUrl(photoFinalImageURL)))// remove AWS keys 
       .error(defaultNoImageDrawable) 
       .placeholder(defaultNoImageDrawable) 
       .dontAnimate() 
       .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
       .into(new ImageViewTarget<GlideDrawable>(photoHolder.photo) { 
        @Override 
        protected void setResource(GlideDrawable resource) { 
        } 

        @Override 
        public void onResourceReady(final GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { 
         //super.onResourceReady(resource, glideAnimation); 
         view.setImageDrawable(resource); 
        } 
       }); 

请建议我有什么办法在滑翔实现。

+0

请在这里上传你的代码 –

+0

对不起,我迟到我重视我的代码加载图像来自AWS S3 – user968571

回答

1

重写GlideUrl类的getCacheKey()方法。该方法设置缓存图像的关键。

这里是如何做到这一点:

//Create a custom class and override the method to remove authentication header from the S3 image url 

public class GlideUrlCustomCacheKey extends GlideUrl { 
    public GlideUrlCustomCacheKey(String url) { 
     super(url); 
    } 

    public GlideUrlCustomCacheKey(String url, Headers headers) { 
     super(url, headers); 
    } 

    public GlideUrlCustomCacheKey(URL url) { 
     super(url); 
    } 

    public GlideUrlCustomCacheKey(URL url, Headers headers) { 
     super(url, headers); 
    } 

    @Override 
    public String getCacheKey() { 
     String url = toStringUrl(); 
     if (url.contains("?")) { 
      return url.substring(0, url.lastIndexOf("?")); 
     } else { 
      return url; 
     } 
    } 
} 

设置与该类获得的URL的ImageView的:

Glide.with(context).load(new GlideUrlCustomCacheKey(buzzUrl)).into(imageView);