欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

缓存

程序员文章站 2022-06-18 23:18:07
...

public class BaseAppliction extends Application {
@Override
public void onCreate() {
super.onCreate();
File cacheFile = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File rootSD = Environment.getExternalStorageDirectory();
cacheFile = new File(rootSD, “pic”);
if (!cacheFile.exists()) {
cacheFile.mkdirs();
}
}

    DiskCache diskCache = null;
    try {
        diskCache = new LruDiskCache(cacheFile, new Md5FileNameGenerator(), 50 * 1024 * 1024);
    } catch (IOException e) {
        e.printStackTrace();
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheOnDisk(true)
            .cacheInMemory(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .showImageOnFail(R.mipmap.ic_launcher)
            .showImageOnLoading(R.mipmap.ic_launcher)
            .showImageForEmptyUri(R.mipmap.ic_launcher)
            .build();

    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
            .diskCache(diskCache)
            .memoryCache(new LruMemoryCache(12 * 1024 * 1024))
            .threadPoolSize(3)
            .defaultDisplayImageOptions(options)
            .build();

    ImageLoader.getInstance().init(configuration);

}

}