public Bitmap getBitmapFromDiskCache(String path) { if (mDiskCache == null) return null; String hashKey = hashKeyForDisk(path); Bitmap bitmap; try { DiskLruCache.Snapshot snapshot = mDiskCache.get(hashKey); if (snapshot == null) { return null; } LogUtils.LOGV(TAG, "ICS disk cache hit"); if (mPauseDiskAccess) { return null; } bitmap = BitmapFactory.decodeStream(snapshot.getInputStream(0)); } catch (IOException e) { LogUtils.LOGE(TAG, "getBitmapFromDiskCache - " + e); bitmap = null; } return bitmap; }
public File getFile() { try { DiskLruCache.Snapshot snapshot = mDiskCache.get(mKey); if (snapshot != null) { return snapshot.getFile(0); } } catch (IOException e) { Log.e(Constants.LOG_TAG, "Could open disk cache for url: " + mKey, e); } return null; }
/** * Adds a bitmap to both memory and disk cache. * * @param data Unique identifier for the bitmap to store * @param value The bitmap drawable to store */ public void addBitmapToCache(String data, BitmapDrawable value) { // BEGIN_INCLUDE(add_bitmap_to_cache) if (data == null || value == null) { return; } // Add to memory cache if (mMemoryCache != null) { if (RecyclingBitmapDrawable.class.isInstance(value)) { // The removed entry is a recycling drawable, so notify it // that it has been added into the memory cache ((RecyclingBitmapDrawable) value).setIsCached(true); } mMemoryCache.put(data, value); } synchronized (mDiskCacheLock) { // Add to disk cache if (mDiskLruCache != null) { final String key = hashKeyForDisk(data); OutputStream out = null; try { DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot == null) { final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); value .getBitmap() .compress(mCacheParams.compressFormat, mCacheParams.compressQuality, out); editor.commit(); out.close(); } } else { snapshot.getInputStream(DISK_CACHE_INDEX).close(); } } catch (final IOException e) { Log.e(TAG, "addBitmapToCache - " + e); } catch (Exception e) { Log.e(TAG, "addBitmapToCache - " + e); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { } } } } // END_INCLUDE(add_bitmap_to_cache) }
@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(); } } }
// basic get public InputStream get(String key) { try { DiskLruCache.Snapshot snapshot = mDiskLruCache.get(Utils.hashKeyForDisk(key)); if (snapshot == null) // not find entry , or entry.readable = false { Log.e(TAG, "not find entry , or entry.readable = false"); return null; } // write READ return snapshot.getInputStream(0); } catch (IOException e) { e.printStackTrace(); return null; } }
/** * Get from disk cache. * * @param data Unique identifier for which item to get * @return The bitmap if found in cache, null otherwise */ public Bitmap getBitmapFromDiskCache(String data) { // BEGIN_INCLUDE(get_bitmap_from_disk_cache) final String key = hashKeyForDisk(data); Bitmap bitmap = null; synchronized (mDiskCacheLock) { while (mDiskCacheStarting) { try { mDiskCacheLock.wait(); } catch (InterruptedException e) { } } if (mDiskLruCache != null) { InputStream inputStream = null; try { final DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot != null) { if (BuildConfig.DEBUG) { Log.d(TAG, "Disk cache hit"); } inputStream = snapshot.getInputStream(DISK_CACHE_INDEX); if (inputStream != null) { FileDescriptor fd = ((FileInputStream) inputStream).getFD(); // Decode bitmap, but we don't want to sample so give // MAX_VALUE as the target dimensions bitmap = ImageResizer.decodeSampledBitmapFromDescriptor( fd, Integer.MAX_VALUE, Integer.MAX_VALUE, this); } } } catch (final IOException e) { Log.e(TAG, "getBitmapFromDiskCache - " + e); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { } } } return bitmap; } // END_INCLUDE(get_bitmap_from_disk_cache) }
/** * 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; }
/** * Adds a new image to the memory and disk caches * * @param data The key used to store the image * @param bitmap The {@link Bitmap} to cache */ public void addBitmapToCache(final String data, final Bitmap bitmap) { if (data == null || bitmap == null) { return; } // Add to memory cache addBitmapToMemCache(data, bitmap); // Add to disk cache if (mDiskCache != null) { final String key = hashKeyForDisk(data); OutputStream out = null; try { final DiskLruCache.Snapshot snapshot = mDiskCache.get(key); if (snapshot == null) { final DiskLruCache.Editor editor = mDiskCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); bitmap.compress(COMPRESS_FORMAT, COMPRESS_QUALITY, out); editor.commit(); out.close(); flush(); } } else { snapshot.getInputStream(DISK_CACHE_INDEX).close(); } } catch (final IOException e) { Log.e(TAG, "addBitmapToCache - " + e); } finally { try { if (out != null) { out.close(); out = null; } } catch (final IOException e) { Log.e(TAG, "addBitmapToCache - " + e); } catch (final IllegalStateException e) { Log.e(TAG, "addBitmapToCache - " + e); } } } }
/** * Adds a bitmap to both memory and disk cache * * @param data Unique identifier for the bitmap to store * @param bitmap The bitmap to store */ public void addBitmapToCache(String data, Bitmap bitmap) { if (data == null || bitmap == null) { return; } // Add the bitmap to memory cache if (mMemoryCache != null && mMemoryCache.get(data) == null) { mMemoryCache.put(data, bitmap); } // Add the bitmap to disk cache synchronized (mDiskCacheLock) { final String key = hashKeyForDisk(data); OutputStream outputStream = null; try { DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot == null) { final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { outputStream = editor.newOutputStream(DISK_CACHE_INDEX); bitmap.compress( mCacheParams.compressFormat, mCacheParams.compressQuality, outputStream); editor.commit(); outputStream.close(); } } else { snapshot.getInputStream(DISK_CACHE_INDEX).close(); ; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
/** * 从缓存中读数据 * * @param name 表名 * @return JSON数据串 */ public String read(String name) { String s = ""; StringBuilder stringBuilder = new StringBuilder(""); try { DiskLruCache.Snapshot snapshot = diskLruCache.get(name); if (snapshot != null) { InputStream in = snapshot.getInputStream(0); byte[] bytes = new byte[1024]; int length; while ((length = in.read(bytes)) > 0) { stringBuilder.append(new String(bytes, 0, length)); } s = stringBuilder.toString(); } else { Log.e("Test", "found fail"); } } catch (IOException e) { e.printStackTrace(); } return s; }
/** * Fetches a cached image from the disk cache * * @param data Unique identifier for which item to get * @return The {@link Bitmap} if found in cache, null otherwise */ public final Bitmap getBitmapFromDiskCache(final String data) { if (data == null) { return null; } // Check in the memory cache here to avoid going to the disk cache less // often if (getBitmapFromMemCache(data) != null) { return getBitmapFromMemCache(data); } waitUntilUnpaused(); final String key = hashKeyForDisk(data); if (mDiskCache != null) { InputStream inputStream = null; try { final DiskLruCache.Snapshot snapshot = mDiskCache.get(key); if (snapshot != null) { inputStream = snapshot.getInputStream(DISK_CACHE_INDEX); if (inputStream != null) { final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); if (bitmap != null) { return bitmap; } } } } catch (final IOException e) { Log.e(TAG, "getBitmapFromDiskCache - " + e); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (final IOException e) { } } } return null; }
/** * 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; }
/** * Get bitmap from disk cache * * @param data Unique identifier for which item to get * @return The bitmap if found in disk cache, null otherwise */ public Bitmap getBitmapFromDiskCache(String data) { final String key = hashKeyForDisk(data); synchronized (mDiskCacheLock) { while (mDiskCacheStarting) { // 由clearCache()和initDiskCache()的状态而变化 try { mDiskCacheLock.wait(); } catch (InterruptedException e) { } } if (mDiskLruCache != null) { InputStream inputStream = null; try { final DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot != null) { if (BuildConfig.DEBUG) { Log.d(TAG, "Disk Cache Hit!!"); } inputStream = snapshot.getInputStream(DISK_CACHE_INDEX); if (inputStream != null) { final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } } catch (IOException e) { // TODO Auto-generated catch block Log.e(TAG, "getBitmapFromDiskCache--" + e); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { } } } return null; } }
private FilterInputStream getFromCache(String url) throws Exception { DiskLruCache cache = DiskLruCache.open(CommonUtil.getImageSavePath(), 1, 2, 2 * 1024 * 1024); cache.flush(); String key = Util.hash(url); final DiskLruCache.Snapshot snapshot; try { snapshot = cache.get(key); if (snapshot == null) { return null; } } catch (IOException e) { return null; } FilterInputStream bodyIn = new FilterInputStream(snapshot.getInputStream(1)) { @Override public void close() throws IOException { snapshot.close(); super.close(); } }; return bodyIn; }
/** * The main process method, which will be called by the ImageWorker in the AsyncTask background * thread. * * @param data The data to load the bitmap, in this case, a regular http URL * @return The downloaded and resized bitmap */ private Bitmap processBitmap(String data) { if (CacheConfig.DEBUG) { Log.d(TAG, "processBitmap - " + data); } final String key = ImageCache.hashKeyForDisk(data); FileDescriptor fileDescriptor = null; FileInputStream fileInputStream = null; DiskLruCache.Snapshot snapshot; synchronized (mHttpDiskCacheLock) { // Wait for disk cache to initialize while (mHttpDiskCacheStarting) { try { mHttpDiskCacheLock.wait(); } catch (InterruptedException e) { } } if (mHttpDiskCache != null) { try { snapshot = mHttpDiskCache.get(key); if (snapshot == null) { if (CacheConfig.DEBUG) { Log.d(TAG, "processBitmap, not found in http cache, downloading..."); } DiskLruCache.Editor editor = mHttpDiskCache.edit(key); if (editor != null) { if (downloadUrlToStream(data, editor.newOutputStream(DISK_CACHE_INDEX))) { editor.commit(); } else { editor.abort(); } } snapshot = mHttpDiskCache.get(key); } if (snapshot != null) { fileInputStream = (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX); fileDescriptor = fileInputStream.getFD(); } } catch (IOException e) { Log.e(TAG, "processBitmap - " + e); } catch (IllegalStateException e) { Log.e(TAG, "processBitmap - " + e); } finally { if (fileDescriptor == null && fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { } } } } } Bitmap bitmap = null; if (fileDescriptor != null) { bitmap = decodeSampledBitmapFromDescriptor( fileDescriptor, mImageWidth, mImageHeight, getImageCache()); } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { } } return bitmap; }