Search code examples
androidandroid-6.0-marshmallowandroid-glideimage-loading

Glide not creating cache directions on Android M


How does Glide caching work with android M . I'm unable to see the cache directory in my external storage. I understand that this would need an external storage permission, but post permission how does reinitialization of the external cache work.

Here is my 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: This was working fine until marshmallow I targeted marshmallow.


Solution

  • After interacting with Glide developers, I decided to implement a strategy where in I check for availability of the storage permission with each request and depending on the provision of the same I choose whether to use diskCacheStrategy to NONEor ALL.

    This seems to be graceful way to tradeoff between network usage and storage.

    An alternative would be to write your own LruDiskCacheFactory which is permission aware.