如何利用缓存动态影像的URI的图像? [英] How to cache images using dynamic image URIs?
问题描述
我如何设置href=\"https://github.com/nostra13/Android-Universal-Image-Loader\" rel=\"nofollow\"> Android的通用图像装载机,从而
How can I setup the Android Universal Image Loader to load dynamic image URIs?
例如:
的URI都必须重新presenting相同的远程图像 image.jpg的
:
Both URIs must be representing the same remote image image.jpg
:
参考:
有时,你可能不希望使用的图像的URL作为缓存键,因为
URL的一部分是动态的(即:用于访问控制的目的)
Sometime, you may not want to use the image URL as cache key because part of the URL is dynamic (i.e.: for access control purpose)
我现在用的是我的SDWebImage的iOS应用程序中,我真的需要一个类似的功能,以便能够使用其UIL Android版本中。
I am using the SDWebImage inside my iOS application and I really need a similar feature to be able to use the UIL inside its Android version.
推荐答案
我想你可以使用此内存高速缓存装饰:
I think you can use this memory cache decorator:
public class CustomMemoryCache implements MemoryCacheAware<String, Bitmap> {
private final MemoryCacheAware<String, Bitmap> cache;
public CustomMemoryCache(MemoryCacheAware<String, Bitmap> cache) {
this.cache = cache;
}
@Override
public boolean put(String key, Bitmap value) {
return cache.put(cleanKey(key), value);
}
@Override
public Bitmap get(String key) {
return cache.get(cleanKey(key));
}
@Override
public void remove(String key) {
cache.remove(cleanKey(key));
}
@Override
public Collection<String> keys() {
return cache.keys();
}
@Override
public void clear() {
cache.clear();
}
private String cleanKey(String key) {
return key.substring(0, key.lastIndexOf("?")) +
key.substring(key.lastIndexOf("_"));
// original cache key is like "[imageUri]_[width]x[height]"
}
}
然后换任何现成的内存缓存实现,并将其设置到配置。
例如:
Then wrap any ready memory cache implementation and set it into the configuration. For example:
LruMemoryCache memoryCache = new LruMemoryCache(memoryCacheSize);
CustomMemoryCache memoryCacheDecorator = new CustomMemoryCache(memoryCache);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
...
.memoryCache(memoryCacheDecorator)
...
.build();
这篇关于如何利用缓存动态影像的URI的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!