Example #1
0
 public void clearDiskCache(String uri) {
   synchronized (mDiskCacheLock) {
     if (mDiskLruCache != null && !mDiskLruCache.isClosed()) {
       try {
         mDiskLruCache.remove(uri);
       } catch (Throwable e) {
         LogUtils.e(e.getMessage(), e);
       }
     }
   }
 }
Example #2
0
 /**
  * Closes the disk cache associated with this ImageCache object. Note that this includes disk
  * access so this should not be executed on the main/UI thread.
  */
 public void close() {
   synchronized (mDiskCacheLock) {
     if (mDiskLruCache != null) {
       try {
         if (!mDiskLruCache.isClosed()) {
           mDiskLruCache.close();
           mDiskLruCache = null;
         }
       } catch (Throwable e) {
         LogUtils.e(e.getMessage(), e);
       }
     }
   }
 }
Example #3
0
 public void clearDiskCache() {
   synchronized (mDiskCacheLock) {
     if (mDiskLruCache != null && !mDiskLruCache.isClosed()) {
       try {
         mDiskLruCache.delete();
       } catch (Throwable e) {
         LogUtils.e(e.getMessage(), e);
       }
       mDiskLruCache = null;
       isDiskCacheReadied = false;
     }
   }
   initDiskCache();
 }
Example #4
0
 /**
  * Flushes the disk cache associated with this ImageCache object. Note that this includes disk
  * access so this should not be executed on the main/UI thread.
  */
 public void flush() {
   synchronized (mDiskCacheLock) {
     if (mDiskLruCache != null) {
       try {
         mDiskLruCache.flush();
       } catch (Throwable e) {
         LogUtils.e(e.getMessage(), e);
       }
     }
   }
 }
Example #5
0
  /**
   * Get the bitmap from disk cache.
   *
   * @param uri
   * @param config
   * @return
   */
  public Bitmap getBitmapFromDiskCache(String uri, BitmapDisplayConfig config) {
    if (uri == null || !globalConfig.isDiskCacheEnabled()) return null;
    synchronized (mDiskCacheLock) {
      while (!isDiskCacheReadied) {
        try {
          mDiskCacheLock.wait();
        } catch (Throwable e) {
        }
      }
    }
    if (mDiskLruCache != null) {
      LruDiskCache.Snapshot snapshot = null;
      try {
        snapshot = mDiskLruCache.get(uri);
        if (snapshot != null) {
          Bitmap bitmap = null;
          if (config == null || config.isShowOriginal()) {
            bitmap =
                BitmapDecoder.decodeFileDescriptor(
                    snapshot.getInputStream(DISK_CACHE_INDEX).getFD());
          } else {
            bitmap =
                BitmapDecoder.decodeSampledBitmapFromDescriptor(
                    snapshot.getInputStream(DISK_CACHE_INDEX).getFD(),
                    config.getBitmapMaxSize(),
                    config.getBitmapConfig());
          }

          bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
          addBitmapToMemoryCache(uri, config, bitmap, mDiskLruCache.getExpiryTimestamp(uri));
          return bitmap;
        }
      } catch (Throwable e) {
        LogUtils.e(e.getMessage(), e);
      } finally {
        IOUtils.closeQuietly(snapshot);
      }
    }
    return null;
  }
Example #6
0
  /**
   * Initializes the disk cache. Note that this includes disk access so this should not be executed
   * on the main/UI thread. By default an ImageCache does not initialize the disk cache when it is
   * created, instead you should call initDiskCache() to initialize it on a background thread.
   */
  public void initDiskCache() {
    if (!globalConfig.isDiskCacheEnabled()) return;

    // Set up disk cache
    synchronized (mDiskCacheLock) {
      if (mDiskLruCache == null || mDiskLruCache.isClosed()) {
        File diskCacheDir = new File(globalConfig.getDiskCachePath());
        if (diskCacheDir.exists() || diskCacheDir.mkdirs()) {
          long availableSpace = OtherUtils.getAvailableSpace(diskCacheDir);
          long diskCacheSize = globalConfig.getDiskCacheSize();
          diskCacheSize = availableSpace > diskCacheSize ? diskCacheSize : availableSpace;
          try {
            mDiskLruCache = LruDiskCache.open(diskCacheDir, 1, 1, diskCacheSize);
            mDiskLruCache.setFileNameGenerator(globalConfig.getFileNameGenerator());
          } catch (Throwable e) {
            mDiskLruCache = null;
            LogUtils.e(e.getMessage(), e);
          }
        }
      }
      isDiskCacheReadied = true;
      mDiskCacheLock.notifyAll();
    }
  }
Example #7
0
 /**
  * Get the bitmap file from disk cache.
  *
  * @param uri Unique identifier for which item to get
  * @return The file if found in cache.
  */
 public File getBitmapFileFromDiskCache(String uri) {
   if (mDiskLruCache != null) {
     return mDiskLruCache.getCacheFile(uri, DISK_CACHE_INDEX);
   }
   return null;
 }
Example #8
0
  public Bitmap downloadBitmap(
      String uri, BitmapDisplayConfig config, final BitmapUtils.BitmapLoadTask<?> task) {

    BitmapMeta bitmapMeta = new BitmapMeta();

    OutputStream outputStream = null;
    LruDiskCache.Snapshot snapshot = null;

    try {
      Bitmap bitmap = null;
      // try download to disk
      if (globalConfig.isDiskCacheEnabled()) {
        synchronized (mDiskCacheLock) {
          // Wait for disk cache to initialize
          while (!isDiskCacheReadied) {
            try {
              mDiskCacheLock.wait();
            } catch (Throwable e) {
            }
          }
        }

        if (mDiskLruCache != null) {
          try {
            snapshot = mDiskLruCache.get(uri);
            if (snapshot == null) {
              LruDiskCache.Editor editor = mDiskLruCache.edit(uri);
              if (editor != null) {
                outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
                bitmapMeta.expiryTimestamp =
                    globalConfig.getDownloader().downloadToStream(uri, outputStream, task);
                if (bitmapMeta.expiryTimestamp < 0) {
                  editor.abort();
                  return null;
                } else {
                  editor.setEntryExpiryTimestamp(bitmapMeta.expiryTimestamp);
                  editor.commit();
                }
                snapshot = mDiskLruCache.get(uri);
              }
            }
            if (snapshot != null) {
              bitmapMeta.inputStream = snapshot.getInputStream(DISK_CACHE_INDEX);
              bitmap = decodeBitmapMeta(bitmapMeta, config);
              if (bitmap == null) {
                bitmapMeta.inputStream = null;
                mDiskLruCache.remove(uri);
              }
            }
          } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
          }
        }
      }

      // try download to memory stream
      if (bitmap == null) {
        outputStream = new ByteArrayOutputStream();
        bitmapMeta.expiryTimestamp =
            globalConfig.getDownloader().downloadToStream(uri, outputStream, task);
        if (bitmapMeta.expiryTimestamp < 0) {
          return null;
        } else {
          bitmapMeta.data = ((ByteArrayOutputStream) outputStream).toByteArray();
          bitmap = decodeBitmapMeta(bitmapMeta, config);
        }
      }

      if (bitmap != null) {
        bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
        if (config != null && config.getImageFactory() != null) {
          bitmap = config.getImageFactory().createBitmap(bitmap);
        }
        addBitmapToMemoryCache(uri, config, bitmap, bitmapMeta.expiryTimestamp);
      }
      return bitmap;
    } catch (Throwable e) {
      LogUtils.e(e.getMessage(), e);
    } finally {
      IOUtils.closeQuietly(outputStream);
      IOUtils.closeQuietly(snapshot);
    }

    return null;
  }
Example #9
0
 public void setDiskCacheFileNameGenerator(FileNameGenerator fileNameGenerator) {
   if (mDiskLruCache != null && fileNameGenerator != null) {
     mDiskLruCache.setFileNameGenerator(fileNameGenerator);
   }
 }
Example #10
0
 public void setDiskCacheSize(int maxSize) {
   if (mDiskLruCache != null) {
     mDiskLruCache.setMaxSize(maxSize);
   }
 }