Exemplo n.º 1
0
 public Editor open(String key) throws IOException {
   if (null != mDiskLruCache) {
     return mDiskLruCache.edit(key);
   } else {
     Log.e(TAG, "mDiskLruCache is null");
     return null;
   }
 }
Exemplo n.º 2
0
  /**
   * Adds a bitmap to both memory and disk cache
   *
   * @param key Unique identifier for the bitmap to store
   * @param bitmap The bitmap to store
   */
  public void write(String key, Bitmap bitmap) {
    if (key == null || bitmap == null) {
      return;
    }

    synchronized (mDiskCacheLock) {

      // Add to disk cache
      if (mDiskLruCache != null) {
        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);
              bitmap.compress(DEFAULT_COMPRESS_FORMAT, DEFAULT_COMPRESS_QUALITY, out);
              editor.commit();
              out.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) {
          }
        }
      }
    }
  }