コード例 #1
0
  /**
   * Puts bitmap into the cache.
   *
   * @param key the key for the bitmap.
   * @param bitmap the bitmap.
   * @return true if successful; false otherwise.
   */
  private boolean put(String key, Bitmap bitmap) {
    boolean isSuccessful = true;

    // Get unique filename based on key.
    final String filename = StorageHelper.generateValidFilename(key);
    if (TextHelper.isValid(filename)) {
      final File file = new File(mDiskCacheDir, filename);

      // Store PNG in disk cache.
      try {
        final OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        isSuccessful = ImageHelper.writePng(bitmap, outputStream);
        outputStream.flush();
        outputStream.close();
      } catch (FileNotFoundException e) {
        isSuccessful = false;
      } catch (IOException e) {
        isSuccessful = false;
      }

      if (isSuccessful) {
        // Read PNG from disk cache and put bitmap in memory cache.
        Bitmap storedBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        if (storedBitmap != null) {
          mMemCache.put(key, storedBitmap);
        } else {
          isSuccessful = false;
        }
      }
    }

    return isSuccessful;
  }
コード例 #2
0
  /**
   * Removes bitmap from the cache.
   *
   * @param key the key for the bitmap.
   * @return true if successful; false otherwise.
   */
  private boolean remove(String key) {
    boolean isSuccessful = true;

    // Get unique filename based on key.
    final String filename = StorageHelper.generateValidFilename(key);
    if (TextHelper.isValid(filename)) {
      // Remove PNG from disk cache.
      final File file = new File(mDiskCacheDir, filename);
      isSuccessful = file.delete();

      // Remove bitmap from memory cache.
      mMemCache.remove(key);
    }

    return isSuccessful;
  }
コード例 #3
0
  /**
   * Gets bitmap from the cache.
   *
   * @param key the key for the bitmap.
   * @return the bitmap; or null if failed.
   */
  private Bitmap get(String key) {
    // Try to get bitmap from memory cache.
    Bitmap bitmap = mMemCache.get(key);

    // Try to get PNG from disk cache if bitmap not found in memory cache.
    if (bitmap == null) {
      // Get unique filename based on key.
      String filename = StorageHelper.generateValidFilename(key);
      if (TextHelper.isValid(filename)) {
        final File file = new File(mDiskCacheDir, filename);
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        if (bitmap != null) {
          // Put bitmap in memory cache.
          mMemCache.put(key, bitmap);
        }
      }
    }

    return bitmap;
  }