滑行.缓存到外部存储(SD卡) [英] Glide. Cache to External Storage (SD card)
问题描述
我使用 Glide 在我的android应用程序中加载图片.
在3.5更新之后,开发人员提供了 GlideModule 界面.
根据这篇文章(磁盘缓存),我可以使用setDiskCache
方法和ExternalCacheDiskCacheFactory
.
但是我没有看到任何区别.所有缓存仍在默认缓存目录中的内部存储上.
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清单:
<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.with(context)
.load("some_url")
.dontAnimate()
.centerCrop()
.override(100, 100)
.into(holder.iv_image);
推荐答案
设置值为setDiskCache
的GlideBuilder
可以将ExternalStorageDirectory设置为缓存目录.
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);
}
希望它能为您提供帮助.
Hope its help you.
这篇关于滑行.缓存到外部存储(SD卡)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!