Skip to content

Instantly share code, notes, and snippets.

@TWiStErRob
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TWiStErRob/d8da8038fc85d6363ad8 to your computer and use it in GitHub Desktop.
Save TWiStErRob/d8da8038fc85d6363ad8 to your computer and use it in GitHub Desktop.
Glide DiskCache.Factory implementation to use the context-provided externalCacheDir
import java.io.File;
import android.content.Context;
import com.bumptech.glide.load.engine.cache.*;
public final class ExternalCacheDiskCacheFactory implements DiskCache.Factory {
private final Context context;
private final String diskCacheName;
private final int diskCacheSize;
public ExternalCacheDiskCacheFactory(Context context, int diskCacheSize) {
this(context, null /*diskCacheName*/, diskCacheSize);
}
public ExternalCacheDiskCacheFactory(Context context, String diskCacheName, int diskCacheSize) {
this.context = context;
this.diskCacheName = diskCacheName;
this.diskCacheSize = diskCacheSize;
}
@Override
public DiskCache build() {
DiskCache diskCache = null;
final File cacheDir;
if (diskCacheName != null) {
cacheDir = new File(context.getExternalCacheDir(), diskCacheName);
} else {
cacheDir = context.getExternalCacheDir();
}
if (cacheDir != null) {
diskCache = DiskLruCacheWrapper.get(cacheDir, diskCacheSize);
}
// only needed before 3.6.0, see https://github.com/bumptech/glide/issues/374
// if (diskCache == null) {
// diskCache = new DiskCacheAdapter();
// }
return diskCache;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment