Glide. Cache to External Storage (SD card)

不羁的心 提交于 2020-01-01 01:54:12

问题


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 using:

Glide.with(context)
.load("some_url")
.dontAnimate()
.centerCrop()
.override(100, 100)
.into(holder.iv_image);

回答1:


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.



来源:https://stackoverflow.com/questions/34253434/glide-cache-to-external-storage-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!