I use Glide for loading Images in my android application.
After 3.5 update, developers provided GlideModule interface.
According to this article (Disk Cache) I can set cache directory, using setDiskCache
method and ExternalCacheDiskCacheFactory
.
But I doesn't see any difference. All cache still on the Internal storage in default cache directory.
build.gradle:
dependencies {
...
compile 'com.github.bumptech.glide:glide:3.6.1'
}
Android Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application ...>
<meta-data
android:name="com.myapp.GlideConfig"
android:value="GlideModule" />
</application>
GlideConfig.java:
public class GlideConfig implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.e("GlideConfig", "MEDIA_MOUNTED");
builder.setDiskCache(
context.getString(R.string.app_name),
419430400));//400Mb
//Environment.getExternalStorageDirectory().getPath()
// + "/"
// + context.getString(R.string.app_name)
}
else {
Log.e("GlideConfig", "!MEDIA_MOUNTED");
}
}
@Override
public void registerComponents(Context context, Glide glide) {
}
}
proguard-rules.pro:
...
-keepnames class * com.myapp.GlideConfig
Glide usage:
Glide.with(context)
.load("some_url")
.dontAnimate()
.centerCrop()
.override(100, 100)
.into(holder.iv_image);
To setup GlideBuilder
with value of setDiskCache
You can set your ExternalStorageDirectory as a cache directory.
if (!Glide.isSetup()) {
GlideBuilder gb = new GlideBuilder(this);
DiskCache dlw = DiskLruCacheWrapper.get(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/YourCacheDirectory/"), 250 * 1024 * 1024);
gb.setDiskCache(dlw);
Glide.setup(gb);
}
Check Can't set GlideBuilder
Hope its help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With