18

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);
Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63
  • What do you consider to be the 'internal storage default cache directory'? The code looks OK to me, so I'm wondering if this is just a matter of confusion over what Android considers to be internal/external storage.. – Tim Malseed Dec 18 '15 at 03:27
  • @TimMalseed, there are no confusion. Glide and other image loaders put image-cache to '/data/data/com.package/cache' (Internal storage) by default. But 'GlideModule' feature should do the trick. Maybe I'm missing something. – Volodymyr Kulyk Dec 18 '15 at 10:25
  • What device/ROM are you testing this on? You cannot say 'there is no confusion'. When it comes to internal/external storage on Android, even Google are confused. – Tim Malseed Dec 20 '15 at 08:26
  • @TimMalseed Samsung Galaxy S2. – Volodymyr Kulyk Dec 20 '15 at 12:45
  • 1
    If you put a log statement in applyOptions in your GlideModule is anything printed? Can you narrow down the problem to your Factory or the GlideModule – Sam Judd Dec 22 '15 at 00:49
  • @SamJudd yes, see GlideConfig.java update. Log "GlideConfig" "MEDIA_MOUNTED" on Application started. – Volodymyr Kulyk Dec 22 '15 at 08:01

1 Answers1

12

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.

Community
  • 1
  • 1
pRaNaY
  • 24,642
  • 24
  • 96
  • 146