Пример #1
0
  /**
   * Load an image specified by the data parameter into an ImageView (override {@link
   * ImageWorker#processBitmap(Object)} to define the processing logic). A memory and disk cache
   * will be used if an {@link ImagesCache} has been added using {@link
   * ImageWorker#addImageCache(FragmentManager, ImagesCache.ImageCacheParams)}. If the image is
   * found in the memory cache, it is set immediately, otherwise an {@link AsyncTask} will be
   * created to asynchronously load the bitmap.
   *
   * @param data The URL of the image to download.
   * @param imageView The ImageView to bind the downloaded image to.
   */
  public void loadImage(Object data, ImageViewWithProcessIndicator imageView) {
    if (data == null) {
      return;
    }

    BitmapDrawable value = null;

    if (mImageCache != null) {
      value = mImageCache.getBitmapFromMemCache(String.valueOf(data));
    }

    if (value != null) {
      // Bitmap found in memory cache
      imageView.getImageView().setImageDrawable(value);
      imageView.showImage();
    } else if (cancelPotentialWork(data, imageView)) {
      final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
      final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task);
      imageView.getImageView().setImageDrawable(asyncDrawable);
      imageView.showAsLoading();
      // NOTE: This uses a custom version of AsyncTask that has been pulled from the
      // framework and slightly modified. Refer to the docs at the top of the class
      // for more info on what was changed.
      task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR, data);
    }
  }
Пример #2
0
  public DownloadImageTask(BaseAdapter adapter, int desiredWidth, int desiredHeight) {
    this.adapter = adapter;

    this.cache = ImagesCache.getInstance();

    this.desiredWidth = desiredWidth;

    this.desiredHeight = desiredHeight;
  }
Пример #3
0
  private Bitmap getImage(String imageUrl) {
    if (cache.getImageFromWarehouse(imageUrl) == null) {
      BitmapFactory.Options options = new BitmapFactory.Options();

      options.inJustDecodeBounds = true;

      options.inSampleSize = inSampleSize;

      try {
        URL url = new URL(imageUrl);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        InputStream stream = connection.getInputStream();

        image = BitmapFactory.decodeStream(stream, null, options);

        int imageWidth = options.outWidth;

        int imageHeight = options.outHeight;

        if (imageWidth > desiredWidth || imageHeight > desiredHeight) {
          System.out.println("imageWidth:" + imageWidth + ", imageHeight:" + imageHeight);

          inSampleSize = inSampleSize + 2;

          getImage(imageUrl);
        } else {
          options.inJustDecodeBounds = false;

          connection = (HttpURLConnection) url.openConnection();

          stream = connection.getInputStream();

          image = BitmapFactory.decodeStream(stream, null, options);

          return image;
        }
      } catch (Exception e) {
        Log.e("getImage", e.toString());
      }
    }

    return image;
  }
Пример #4
0
  @Override
  protected void onPostExecute(Bitmap result) {
    super.onPostExecute(result);

    if (result != null) {
      cache.addImageToWarehouse(imageUrl, result);

      if (ivImageView != null) {
        ivImageView.setImageBitmap(result);
      } else {

      }

      if (adapter != null) {
        adapter.notifyDataSetChanged();
      }
    }
  }
Пример #5
0
 protected ImageWorker(Context context) {
   mResources = context.getResources();
   mImageCache = ImagesCache.getInstance();
 }
Пример #6
0
 protected void closeCacheInternal() {
   if (mImageCache != null) {
     mImageCache.close();
     mImageCache = null;
   }
 }
Пример #7
0
 protected void flushCacheInternal() {
   if (mImageCache != null) {
     mImageCache.flush();
   }
 }
Пример #8
0
 protected void clearCacheInternal() {
   if (mImageCache != null) {
     mImageCache.clearCache();
   }
 }
Пример #9
0
 /**
  * Adds an {@link ImagesCache} to this {@link ImageWorker} to handle disk and memory bitmap
  * caching.
  *
  * @param fragmentManager
  * @param cacheParams The cache parameters to use for the image cache.
  */
 public void addImageCache() {
   mImageCache = ImagesCache.getInstance();
   new CacheAsyncTask().execute(MESSAGE_INIT_DISK_CACHE);
 }