Exemplo n.º 1
0
 @Override
 public void onDownloadComplete(
     BitmapWorkerTask caller, String url, @Nullable Bitmap result, boolean wasCancelled) {
   Log.d("ImageLoader", "Download complete: " + url);
   // When a download is completed, if the task wasn't cancelled the content is queued
   //  to be written to cache. If the task was cancelled, the Bitmap might be corrupt.
   if (!wasCancelled && result != null) {
     MemoryCache.instance().addBitmapToMemoryCache(url, result);
     queueWriteRequest(new WriteRequest(result, url));
   }
   // The request is closed
   closeRequest();
 }
Exemplo n.º 2
0
    @Override
    protected void onProgressUpdate(LoadRequest... values) {
      // For each published request
      for (LoadRequest request : values) {
        // The bitmap needs to be downloaded
        if (request.mResult == null) {
          // If the download is NOT to be performed
          if (request.mFlinging) {
            // The placeholder is set to the view and the request is closed.
            request.mImageView.setImageBitmap(mPlaceHolderBitmap);
            closeRequest();
          }
          // If the download is to be performed
          else if (cancelPotentialWork(request.mUrl, request.mImageView)) {
            BitmapWorkerTask task = getBitmapWorkerTask(request.mImageView);
            // If the reference was missed and picked by the GC, the task will
            //  get killed and will never call onDownloadComplete(), so the
            //  request needs to get closed here.
            if (task != null) {
              closeRequest();
            }

            // The proper task is created
            task = new BitmapWorkerTask(mContext, request.mImageView, this);
            // An AsyncDrawable is created with the selected configuration and set
            //  as the drawable of the ImageView
            final AsyncDrawable asyncDrawable;
            if (request.mUsePlaceholder) {
              asyncDrawable = new AsyncDrawable(mContext.getResources(), mPlaceHolderBitmap, task);
            } else {
              asyncDrawable = new AsyncDrawable(mContext.getResources(), task);
            }
            request.mImageView.setImageDrawable(asyncDrawable);

            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, request.mUrl);
          }
        } else {
          MemoryCache.instance().addBitmapToMemoryCache(request.mUrl, request.mResult);
          request.mImageView.setImageBitmap(request.mResult);
          closeRequest();
        }
      }
    }
Exemplo n.º 3
0
  /**
   * Loads the bitmap at the provided url, but checks the cache first.
   *
   * @param view the view to where the bitmap shall be set.
   * @param url the urk of the bitmap. This acts as a key for the cache.
   * @param flinging avoid downloading on cache miss.
   * @param usePlaceholder use a placeholder while the bitmap loads.
   */
  public static void loadBitmap(
      ImageView view, String url, boolean flinging, boolean usePlaceholder) {
    // 1.- Check memory cache
    Bitmap bitmap = MemoryCache.instance().getBitmapFromMemCache(url);
    // 2.- On hit, load, on miss, check disk cache
    if (bitmap != null) {
      Log.d("MemoryCache", "Hit: " + url);
      view.setImageBitmap(bitmap);
    } else {
      Log.d("MemoryCache", "Miss: " + url);

      // Add to queue and start the task if necessary
      queueLoadRequest(new LoadRequest(view, url, flinging, usePlaceholder));
      if (workerTask == null) {
        File dir = new File(mContext.getCacheDir().getPath() + File.separator + DISK_CACHE_SUB_DIR);
        workerTask = new CacheWorkerTask();
        workerTask.execute(dir);
      }
    }
  }