Glide 未在 Android M 上创建缓存方向

Glide not creating cache directions on Android M

Glide 缓存如何与 android M 一起工作。我无法在外部存储中看到缓存目录。我知道这需要外部存储许可,但是 post 许可如何重新初始化外部缓存。

这是我的 GlideModule

public class GlideDefaultModule implements GlideModule {

    private static GlideDefaultModule glideDefaultModule;

    private GlideBuilder glideBuilder;

    public static GlideDefaultModule getModule() {
        return glideDefaultModule;
    }


    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        glideDefaultModule = this;
        this.glideBuilder = builder;

        MemorySizeCalculator calculator = new MemorySizeCalculator(context);
        int defaultMemoryCacheSize = calculator.getMemoryCacheSize();

        int cutOff = (int) (Runtime.getRuntime().maxMemory() / 6f); // 16% of total memory for bitmaps

        if (cutOff < 0 || cutOff > defaultMemoryCacheSize) {

            cutOff = defaultMemoryCacheSize;
        }

        builder.setMemoryCache(new LruResourceCache(cutOff));
        builder.setBitmapPool(new LruBitmapPool(cutOff));

        Runtime.getRuntime().maxMemory();
        if (BaseUtil.isSDCardAvailable()) {
            builder.setDiskCache(
                    new DiskLruCacheFactory(new DiskLruCacheFactory.CacheDirectoryGetter() {
                        @Override
                        public File getCacheDirectory() {
                            return createDirectoryIfNeeded();
                        }
                    }
                            , BaseConstants.DISK_CACHE_SIZE));
        } else {
            builder.setDiskCache(new InternalCacheDiskCacheFactory(context, BaseConstants.DISK_CACHE_SIZE / 10));
        }


    }

    public File createDirectoryIfNeeded() {

        File folder = new File(Environment.getExternalStorageDirectory() + "/" + BaseConstants.PIC_DIRECTORY);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return folder;


    }

    @Override
    public void registerComponents(Context context, Glide glide) {

    }

    public GlideBuilder getGlideBuilder() {
        return glideBuilder;
    }
}

Ps:在我以棉花糖为目标之前,这一切都很好。

在交互 with Glide developers 之后,我决定实施一种策略,在该策略中,我检查每个请求的存储权限的可用性,并根据相同的规定选择是否使用 diskCacheStrategyNONEALL

这似乎是一种在网络使用和存储之间进行权衡的优雅方式。

另一种方法是编写您自己的 LruDiskCacheFactory,它具有权限意识。