I am using Glide library for image loading. There is lot of images in my app so I want to clear cache once cache size is larger than 50 mb. Can someone help me to do so?
Asked
Active
Viewed 3,259 times
1 Answers
6
Call Glide.get(context).clearDiskCache() on outside the UI thread. (also consider clearMemory() too to prevent surprises after clearing disk cache) this worked for me
new Thread(new Runnable() {
@Override
public void run() {
Glide.get(MainActivity.this).clearDiskCache();
}
}).start();
There is lot of images in my app so I want to clear cache once cache size is larger than 50 mb.
in case you want to put limit 50 mb you can implement glide module
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 {
// Modern device should have 8GB (=7.45GiB) or more!
private static final int SMALL_INTERNAL_STORAGE_THRESHOLD_GIB = 6;
private static final int DISK_CACHE_SIZE_FOR_SMALL_INTERNAL_STORAGE_MIB = 50*1024*1024;
@Override
public void applyOptions(Context context, GlideBuilder builder) {
if (MyApplication.from(context).isTest()) return; // NOTE: StatFs will crash on robolectric.
double totalGiB = getTotalBytesOfInternalStorage() / 1024.0 / 1024.0 / 1024.0;
Log.i(String.format(Locale.US, "Internal Storage Size: %.1fGiB", totalGiB));
if (totalGiB < SMALL_INTERNAL_STORAGE_THRESHOLD_GIB) {
Log.i("Limiting image cache size to " + DISK_CACHE_SIZE_FOR_SMALL_INTERNAL_STORAGE_MIB + "MiB");
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, DISK_CACHE_SIZE_FOR_SMALL_INTERNAL_STORAGE_MIB));
}
}
@Override
public void registerComponents(Context context, Glide glide) {
}
private long getTotalBytesOfInternalStorage() {
// http://stackoverflow.com/a/4595449/1474113
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return getTotalBytesOfInternalStorageWithStatFs(stat);
} else {
return getTotalBytesOfInternalStorageWithStatFsPreJBMR2(stat);
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private long getTotalBytesOfInternalStorageWithStatFs(StatFs stat) {
return stat.getTotalBytes();
}
@SuppressWarnings("deprecation")
private long getTotalBytesOfInternalStorageWithStatFsPreJBMR2(StatFs stat) {
return (long) stat.getBlockSize() * stat.getBlockCount();
}
}
and then in your manifest add it like this
<manifest
...
<application>
<meta-data
android:name="YourPackageNameHere.LimitCacheSizeGlideModule"
android:value="GlideModule" />
...
</application>
</manifest>

Digvijay Singh
- 623
- 9
- 26
-
How I will get know the current cache size so that I will call above method once current cache size will cross 50 MB? – Sapna Sharma Mar 15 '17 at 06:33
-
1Once I set the limit to 50 MB, is there any need to clear cache using Glide.get(MainActivity.this).clearDiskCache(); or library will automatically do that? – Sapna Sharma Mar 15 '17 at 06:55
-
1no @SapnaSharma it will set the limit you need not to clear it manually it will be done automatically – Digvijay Singh Mar 15 '17 at 07:06
-
Okay, Thanks. Let me try this. – Sapna Sharma Mar 15 '17 at 07:08
-
I have implemented GlideModule and added meta-data in manifest. Now when LimitCacheSizeGlideModule will get call as there is no logs(added in above code)showing when I am running this code. – Sapna Sharma Mar 15 '17 at 12:13
-
are you using pro-gaurd in your project? – Digvijay Singh Mar 15 '17 at 12:31
-
Yes I am using pro-gaurd but for debug build I disabled it. – Sapna Sharma Mar 15 '17 at 12:46
-
1Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138109/discussion-between-sapna-sharma-and-digvijay-singh). – Sapna Sharma Mar 15 '17 at 12:47