【问题标题】:Universal Image loader not refresh image if changed from server with same URL如果从具有相同 URL 的服务器更改,通用图像加载器不会刷新图像
【发布时间】:2018-03-19 01:20:55
【问题描述】:
  • 我使用 Universal Image Loader 从服务器加载图像并将其缓存到内存中以便快速加载。
  • 但从服务器端使用相同的 URL 更新图像。
  • 例如,www.example.com/xyz.png 是图像的 URL,当他们需要更新图像时,他们返回相同的 URL 和不同的图像。
  • 在这种情况下,Universal Image Loader 返回先前缓存在内存中的图像(我认为它使用其相关 URL 缓存了图像)。
  • 因此,如果 URL 返回不同的图像,我需要更改图像。

这是我用来加载图片的代码

DisplayImageOption.java

public class DisplayImageOption {

public static DisplayImageOptions getDisplayImage() {
    // .displayer(new RoundedBitmapDisplayer(0))
    return new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_place_holder)
            .showImageForEmptyUri(R.mipmap.icon_place_holder)
            .showImageOnFail(R.mipmap.icon_place_holder)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true).build();
}

public static DisplayImageOptions getDisplayRoundedImage() {
    return new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_place_holder)
            .showImageForEmptyUri(R.mipmap.icon_place_holder)
            .showImageOnFail(R.mipmap.icon_place_holder)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .displayer(new RoundedBitmapDisplayer(100)).build();
    }
}

图片加载代码

ImageLoader.getInstance().displayImage(url, imageView, DisplayImageOption.getDisplayImage());

谢谢

【问题讨论】:

    标签: android universal-image-loader


    【解决方案1】:

    在您的ImageLoaderConfiguration 添加diskCache 选项。

    File cacheDir = StorageUtils.getCacheDirectory(context);
    long cacheAge = 10L;
    
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            .diskCache(new LimitedAgeDiscCache(cacheDir, cacheAge)) // this will make the cache to remain for 10 seconds only
            .build();
    

    然后将其设置为ImageLoader 并使用您的DisplayImageOption 显示图像

    ImageLoader.getInstance().init(config);
    ImageLoader.getInstance().displayImage(url, imageView, DisplayImageOption.getDisplayImage());
    

    它有什么作用? 取自Android-Universal-Image-Loader

    LimitedAgeDiscCache(无限大小的缓存,文件的生命周期有限。如果缓存文件的年龄超过定义的限制,则将从缓存中删除。)

    而这段代码来自 Android-Universal-Image-Loader 的LimitedAgeDiskCache.java 类。

    /**
         * @param cacheDir Directory for file caching
         * @param maxAge   Max file age (in seconds). If file age will exceed this value then it'll be removed on next
         *                 treatment (and therefore be reloaded).
         */
        public LimitedAgeDiskCache(File cacheDir, long maxAge) {
            this(cacheDir, null, DefaultConfigurationFactory.createFileNameGenerator(), maxAge);
    }
    

    您可能也喜欢this 方法。

    【讨论】:

    • 感谢您的回答。我也得到了这种类型的答案,但是此代码清除了我的所有图像缓存,我需要像 UIL 检查图像 URL 及其缓存中的数据,如果数据相同,然后从缓存中加载图像和如果数据不同,则与图像 URL 关联的先前缓存将被清除,然后创建新缓存。
    猜你喜欢
    • 2011-06-04
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2015-06-20
    相关资源
    最近更新 更多