/** * Initializes the disk cache. Note that this includes disk access so this should not be executed * on the main/UI thread. By default an ImageCache does not initialize the disk cache when it is * created, instead you should call initDiskCache() to initialize it on a background thread. */ public void initDiskCache() { // Set up disk cache synchronized (mDiskCacheLock) { if (mDiskLruCache == null || mDiskLruCache.isClosed()) { File diskCacheDir = mCacheParams.diskCacheDir; if (mCacheParams.diskCacheEnabled && diskCacheDir != null) { if (!diskCacheDir.exists()) { diskCacheDir.mkdirs(); } if (getUsableSpace(diskCacheDir) > mCacheParams.diskCacheSize) { try { mDiskLruCache = DiskLruCache.open(diskCacheDir, 1, 1, mCacheParams.diskCacheSize); if (BuildConfig.DEBUG) { Log.d(TAG, "Disk cache initialized"); } } catch (final IOException e) { mCacheParams.diskCacheDir = null; Log.e(TAG, "initDiskCache - " + e); } } } } mDiskCacheStarting = false; mDiskCacheLock.notifyAll(); } }
/** * Initializes the disk cache. Note that this includes disk access so this should not be executed * on the main/UI thread. By default an ImageCache does not initialize the disk cache when it is * created, instead you should call initDiskCache() on a background thread */ public void initDiskCache() { synchronized (mDiskCacheLock) { if (mDiskLruCache == null || mDiskLruCache.isClosed()) { File diskCacheDir = mCacheParams.diskCacheDir; if (mCacheParams.diskCacheEnabled && diskCacheDir != null) { if (!diskCacheDir.exists()) { diskCacheDir.mkdirs(); } if (getUsableSpace(diskCacheDir) > mCacheParams.diskCacheSize) { // 缓存路径可用空间大于默认值 try { mDiskLruCache = DiskLruCache.open(diskCacheDir, 1, 1, mCacheParams.diskCacheSize); if (BuildConfig.DEBUG) { Log.d(TAG, "Disk cache initialized successfully"); } } catch (IOException e) { // TODO Auto-generated catch block mCacheParams.diskCacheDir = null; Log.e(TAG, "initDiskCache-" + e); } } } } // 改变mDiskStarting唤醒getBitmapFromDiskCache mDiskCacheStarting = false; mDiskCacheLock.notifyAll(); } }
/** * Initialize the cache, providing all parameters. * * @param context The context to use * @param cacheParams The cache parameters to initialize the cache */ private void init(Context context, ImageCacheParams cacheParams) { mImageCacheParams = cacheParams; // get a cache floder final File diskCacheDir = DiskLruCache.getDiskCacheDir(context, cacheParams.cachePath, cacheParams.uniqueName); // Set up disk cache if (cacheParams.diskCacheEnabled) { mDiskCache = DiskLruCache.openCache(context, diskCacheDir, cacheParams.diskCacheSize); if (null != mDiskCache) { mDiskCache.setCompressParams(cacheParams.compressFormat, cacheParams.compressQuality); if (cacheParams.clearDiskCacheOnStart) { mDiskCache.clearCache(); } } else { Log.e(TAG, "Can't create DiskCache"); cacheParams.diskCacheEnabled = false; } } // Set up memory cache if (cacheParams.memoryCacheEnabled) { mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) { /** * Measure item size in bytes rather than units which is more practical for a bitmap * cache */ @Override protected int sizeOf(String key, Bitmap bitmap) { return BitmapUtil.getBitmapSize(bitmap); } }; } }