如果从具有相同 URL 的服务器更改,通用图像加载程序不会刷新图像

Universal Image loader not refresh image if changed from server with same 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());

谢谢

在您的 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 (Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.)

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

/**
     * @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 方法。