【问题标题】:How to cache images using dynamic image URIs?如何使用动态图像 URI 缓存图像?
【发布时间】:2013-12-25 05:30:58
【问题描述】:

如何设置 Android Universal Image Loader 来加载动态图像 URI?

例如:

两个 URI 必须代表同一个远程图像image.jpg

参考:

有时,您可能不想使用图像 URL 作为缓存键,因为 URL 的一部分是动态的(即:用于访问控制目的)

SDWebImage - Using cache key filter

我在我的 iOS 应用程序中使用 SDWebImage,我确实需要一个类似的功能才能在其 Android 版本中使用 UIL。

【问题讨论】:

    标签: android image caching universal-image-loader


    【解决方案1】:

    我认为你可以使用这个内存缓存装饰器:

    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]"
        }
    }
    

    然后包装任何现成的内存缓存实现并将其设置到配置中。 例如:

    LruMemoryCache memoryCache = new LruMemoryCache(memoryCacheSize);
    CustomMemoryCache memoryCacheDecorator = new CustomMemoryCache(memoryCache);
    
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        ...
        .memoryCache(memoryCacheDecorator)
        ...
        .build();
    

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 2017-09-07
      • 2021-09-29
      • 2021-03-27
      • 2011-04-12
      相关资源
      最近更新 更多