示例#1
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;
  }
示例#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;
  }