예제 #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);
       }
     }
   }
 }
예제 #2
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;
  }