예제 #1
0
  private FileInputStream read(String fileCacheKey) {
    if (!mDiskCacheReady) {
      initDiskCache();
    }

    synchronized (mDiskCacheLock) {
      while (mDiskCacheStarting) {
        try {
          if (DEBUG) {
            Log.d(TAG, "read wait " + this);
          }
          mDiskCacheLock.wait();
        } catch (InterruptedException e) {
        }
      }
      if (mDiskLruCache != null) {
        InputStream inputStream = null;
        DiskLruCache.Snapshot snapshot = null;
        try {
          snapshot = mDiskLruCache.get(fileCacheKey);

        } catch (final IOException e) {
          Log.e(TAG, "getBitmapFromDiskCache - " + e);
        }

        if (snapshot == null) {
          return null;
        } else {
          inputStream = snapshot.getInputStream(DISK_CACHE_INDEX);
          return (FileInputStream) inputStream;
        }
      }
      return null;
    }
  }
예제 #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) {
          }
        }
      }
    }
  }