Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide. Cache to External Storage (SD card)

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);
like image 592
Volodymyr Kulyk Avatar asked Dec 13 '15 16:12

Volodymyr Kulyk


1 Answers

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.

like image 116
pRaNaY Avatar answered Nov 15 '22 18:11

pRaNaY