@Override public File get(String imageUri) { DiskLruCache.Snapshot snapshot = null; try { snapshot = cache.get(getKey(imageUri)); return snapshot == null ? null : snapshot.getFile(0); } catch (IOException e) { L.e(e); return null; } finally { if (snapshot != null) { snapshot.close(); } } }
/** * Tells whether the key exists in the disk cache. * * @param url the key to check for. * @return true if it exists, false otherwise. */ private static boolean isBitmapInDiskCache(String url) { String key = hashKeyForDisk(url); if (mDiskCache != null) { try { DiskLruCache.Snapshot snapshot = mDiskCache.get(key); if (snapshot == null) { Log.d("DiskCache", "Miss: " + url); return false; } else { Log.d("DiskCache", "Hit: " + url); snapshot.close(); return true; } } catch (IOException iox) { iox.printStackTrace(); } } return false; }
/** * Reads a Bitmap from the disk cache. * * @param url the url of the resource. * @return the decoded Bitmap. */ private static Bitmap readBitmapFromDiskCache(String url) { String key = hashKeyForDisk(url); // This shouldn't happen, but better safe than sorry if (mDiskCache != null) { Bitmap bitmap = null; try { DiskLruCache.Snapshot snapshot = mDiskCache.get(key); if (snapshot != null) { // Load the Bitmap file from the cache and close the snapshot (closing // the snapshot also closes the InputStream) FileInputStream inputStream = (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX); bitmap = BitmapFactory.decodeFileDescriptor(inputStream.getFD()); snapshot.close(); } } catch (IOException iox) { iox.printStackTrace(); } return bitmap; } return null; }