How to increase the cache size in Glide android?

4.8k views Asked by At

According to the documentation ,

The internal cache factory places the disk cache in your application's internal cache directory and sets a maximum size of 250MB.

As I am trying to implement some offline features in my apps, it possibly require cache size larger than 250MB. So does Glide allow to modify the cache size or I need to find out an alternative way of doing this? If so, what mechanism should I follow?

I have seen in the documentation an approach to increase that.

builder.setDiskCache(
new InternalCacheDiskCacheFactory(context, yourSizeInBytes));

How do I implement that in my code?

1

There are 1 answers

0
Anjal Saneen On

See here.

You can set the size of the disk cache using the InternalCacheDiskCacheFactory.

builder.setDiskCache(new InternalCacheDiskCacheFactory(context, yourSizeInBytes));

You can apply in this your project like below:

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.module.GlideModule;
import com.example.MyApplication;

import java.util.Locale;

public class LimitCacheSizeGlideModule implements GlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        if (MyApplication.from(context).isTest()) 
            return; // NOTE: StatFs will crash on robolectric.
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, yourSizeInBytes));

    }

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


}

and then in your manifest add it like this

<manifest

    ...

    <application>

        <meta-data
            android:name="YourPackageNameHere.LimitCacheSizeGlideModule"
            android:value="GlideModule" />

        ...

    </application>
</manifest>