Ejemplo n.º 1
0
    @Override
    protected Bitmap doInBackground(Object... params) {
      InputStream inStream = null;
      try {
        String scaledCacheKey =
            getCacheKey(
                mUrl,
                mDownloadRequest.getListener().getDesiredWidth(),
                mDownloadRequest.getListener().getDesiredHeight());
        Snapshot snapshot = mDiskCache.get(scaledCacheKey);
        if (snapshot != null) {
          inStream = snapshot.getInputStream(0);
          return Bitmaps.safeDecodeStream(inStream);
        }

        snapshot = mDiskCache.get(getCacheKey(mUrl));
        if (snapshot != null) {
          inStream = snapshot.getInputStream(0);
          return Bitmaps.safeDecodeStream(snapshot.getInputStream(0));
        }

        return null;
      } catch (IOException e) {
        e.printStackTrace();
        return null;
      } finally {
        IO.closeQuietly(inStream);
      }
    }
Ejemplo n.º 2
0
  public void queryCache(String url, ImageCacheListener listener, DownloadRequest downloadRequest) {
    if (url == null) {
      listener.onImageNotFound(this, url, downloadRequest);
      return;
    }

    String fullSizeCacheKey = getCacheKey(url);
    String scaledCacheKey = "";

    int desiredWidth = downloadRequest.getListener().getDesiredWidth();
    int desiredHeight = downloadRequest.getListener().getDesiredHeight();

    if (desiredWidth > 0 && desiredHeight > 0) {
      scaledCacheKey = getCacheKey(url, desiredWidth, desiredHeight);

      synchronized (mMemoryCache) {
        Bitmap scaledBitmap = mMemoryCache.get(scaledCacheKey);
        if (scaledBitmap != null) {
          listener.onImageFound(this, scaledBitmap, url, downloadRequest);
          return;
        }
      }
    }

    // First check the in-memory cache...
    synchronized (mMemoryCache) {
      Bitmap cachedBitmap = mMemoryCache.get(fullSizeCacheKey);

      if (cachedBitmap != null) {
        // ...notify listener immediately, no need to go async
        listener.onImageFound(this, cachedBitmap, url, downloadRequest);
        return;
      }
    }

    if (mDiskCache != null) {
      ThreadPoolAsyncTaskRunner.runTaskOnPool(
          ThreadPoolAsyncTaskRunner.THUMBNAIL_THREAD_POOL,
          new BitmapDecoderTask(url, listener, downloadRequest),
          (Object[]) null);

      return;
    }
    listener.onImageNotFound(this, url, downloadRequest);
  }