Пример #1
0
  /**
   * @param url
   * @param cache2Memory 是否缓存至memory中
   * @param callback
   */
  public void downloadImage(
      final String url, final boolean cache2Memory, final ImageCallback callback) {
    if (sDownloadingSet.contains(url)) {
      Log.i("AsyncImageLoader", "###该图片正在下载,不能重复下载!");
      return;
    }

    Bitmap bitmap = impl.getBitmapFromMemory(url);
    if (bitmap != null) {
      if (callback != null) {
        callback.onImageLoaded(bitmap, url);
      }
    } else {
      // 从网络端下载图片
      sDownloadingSet.add(url);
      sExecutorService.submit(
          new Runnable() {
            @Override
            public void run() {
              final Bitmap bitmap = impl.getBitmapFromUrl(url, cache2Memory);
              handler.post(
                  new Runnable() {
                    @Override
                    public void run() {
                      if (callback != null) callback.onImageLoaded(bitmap, url);
                      sDownloadingSet.remove(url);
                    }
                  });
            }
          });
    }
  }
Пример #2
0
  /**
   * @param url
   * @param cacheToMemory is cache to memory
   * @param callback
   */
  public void downloadImage(
      final String url, final boolean cacheToMemory, final ImageCallback callback) {
    if (mDownloadingSet.contains(url)) {
      CommonLog.i(CLASSTAG, "image is downloading!");
      return;
    }

    // if image could be gotten
    Bitmap bitmap = impl.getBitmapFromMemory(url);
    CommonLog.i(CLASSTAG, "getBitmapFromMemory = " + bitmap);

    // if not, download from internet
    if (bitmap != null) {
      if (callback != null) {
        callback.onImageLoaded(bitmap, url);
      }
    } else {
      // download from internet
      mDownloadingSet.add(url);
      mExecutorService.submit(
          new Runnable() {
            @Override
            public void run() {
              final Bitmap bitmap = impl.getBitmapFromUrl(url, cacheToMemory);
              handler.post(
                  new Runnable() {
                    @Override
                    public void run() {
                      if (callback != null) {
                        callback.onImageLoaded(bitmap, url);
                      }
                      mDownloadingSet.remove(url);
                    }
                  });
            }
          });
    }
  }