【问题标题】:Can I force Universal Image Loader to cache only internally?我可以强制 Universal Image Loader 仅在内部缓存吗?
【发布时间】:2014-02-06 06:40:17
【问题描述】:

将缓存保存在外部(SD 卡)会导致它在卸载过程中不会被删除。我不希望这种情况发生,我可以编辑UIL 库,使其仅在内部(应用程序内部)保存缓存。无论如何我都在使用LimitedAgeDiscCache,所以它会在任何给定时间被删除。

如果是,我是否应该将 getOwnCacheDirectory 方法从 StorageUtils.class 更改?

【问题讨论】:

    标签: android android-imageview universal-image-loader


    【解决方案1】:

    您可以使用 ImageLoaderConfiguration.Builder(context) 的方法 diskCache() 将 DiskCache 设置为通用图像加载器。而且这个定制的磁盘缓存可能更喜欢内部缓存。

    我们可以通过 Universal-Image-Loader 的公共方法创建内部存储,它的 StorageUtils。

    例如,

    // false to indicator we don't prefer external storage.
    File cacheDir = StorageUtils.getCacheDirectory(context, false); 
    // to avoid exception, we still prepare a default one as universal image loader
    File reserveCacheDir = StorageUtils.getCacheDirectory(context);
    
    long cacheMaxSize = 15 * 1024 * 1024; // 15 MB
    
    DiskCache diskCache;
    try {
        diskCache = new LruDiscCache(cacheDir, reserveCacheDir, DefaultConfigurationFactory.createFileNameGenerator(), cacheMaxSize, 0);
    } catch (IOException e) {
        e.printStackTrace();
        // if we cannot use LruDiscCache with internal cache, jut use DefaultConfigurationFactory instead
        diskCache = DefaultConfigurationFactory.createDiskCache(context, DefaultConfigurationFactory.createFileNameGenerator(), cacheMaxSize, 0);
    }
    
    ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context)
            .diskCache(diskCache);
    ImageLoaderConfiguration config = builder.build();
    ImageLoader.getInstance().init(config);
    

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2012-12-13
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多