The image cache downloaded by glide
is saved in the app. Sometimes there may be a need to put the cache on the SD card. Of course, my phone does not have an SD card, so I can't test how to set the SD card path.
Add in build.gradle according to the tutorial
implementation 'com.github.bumptech.glide:glide:4.5.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
Create a new class to inherit AppGlideModule
@GlideModule public class GlideCache extends AppGlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { super.applyOptions(context, builder); } @Override public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { super.registerComponents(context, glide, registry); } }
The above @GlideModule must be added
Then modify the code inside applyOptions
@Override public void applyOptions(Context context, GlideBuilder builder) { int diskCacheSizeBytes = 1024 * 1024 * 100; // 100 MB //手机app路径 appRootPath = context.getCacheDir().getPath(); builder.setDiskCache( new DiskLruCacheFactory( getStorageDirectory()+"/GlideDisk", diskCacheSizeBytes ) ); } //外部路径 private String sdRootPath = Environment.getExternalStorageDirectory().getPath(); private String appRootPath = null; private String getStorageDirectory(){ return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? sdRootPath : appRootPath; }
Determine whether there is an external path, set this path if there is, use the internal path if not
Then Build->Make Project, then you can use GlideApp in the code
GlideApp.with(this).load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg").into(imageView);
缓存保存到了指定路径
记得加权限,网络和文件读写权限
网上有很多glide的资料,以后用到慢慢搞