use*_*571 5 android image-caching android-glide
我正在使用从 Amazon S3 加载图像的 Android 应用程序。图像 URL 随令牌和过期密钥随机变化。因此,我无法缓存图像 Glide。
有任何方法可以将 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);
}
});
Run Code Online (Sandbox Code Playgroud)
请建议我有什么方法可以在 Glide 中实现。
覆盖 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用从该类获取的 URL 设置 imageView:
Glide.with(context).load(new GlideUrlCustomCacheKey(buzzUrl)).into(imageView);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1884 次 |
| 最近记录: |