예제 #1
0
  private void doDisplay(
      ImageView imageView,
      String uri,
      BitmapDisplayConfig displayConfig,
      CompressFormat compressFormat) {
    if (TextUtils.isEmpty(uri) || imageView == null) {
      return;
    }

    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
    }

    Bitmap bitmap = null;

    bitmap = globalConfig.getBitmapCache().getBitmapFromMemCache(uri);

    if (bitmap != null) {
      imageView.setImageBitmap(bitmap);

    } else if (!bitmapLoadTaskExist(imageView, uri)) {

      final BitmapLoadTask loadTask = new BitmapLoadTask(imageView, displayConfig);
      // set loading image
      final AsyncBitmapDrawable asyncBitmapDrawable =
          new AsyncBitmapDrawable(
              context.getResources(), displayConfig.getLoadingBitmap(), loadTask);
      imageView.setImageDrawable(asyncBitmapDrawable);

      // load bitmap from uri or diskCache
      loadTask.executeOnExecutor(globalConfig.getBitmapLoadExecutor(), uri, compressFormat);
    }
  }
예제 #2
0
 public static BitmapUtils create(
     Context ctx, String diskCachePath, float memoryCachePercent, int diskCacheSize) {
   if (instance == null) {
     instance = new BitmapUtils(ctx.getApplicationContext(), diskCachePath);
     globalConfig.setMemCacheSizePercent(memoryCachePercent);
     globalConfig.setDiskCacheSize(diskCacheSize);
   }
   return instance;
 }
예제 #3
0
 /**
  * Get the bitmap from memory cache.
  *
  * @param uri Unique identifier for which item to get
  * @param config
  * @return The bitmap if found in cache, null otherwise
  */
 public Bitmap getBitmapFromMemCache(String uri, BitmapDisplayConfig config) {
   if (mMemoryCache != null && globalConfig.isMemoryCacheEnabled()) {
     MemoryCacheKey key = new MemoryCacheKey(uri, config);
     return mMemoryCache.get(key);
   }
   return null;
 }
예제 #4
0
  public <T extends View> void display(
      T container, String uri, BitmapDisplayConfig displayConfig, BitmapLoadCallBack<T> callBack) {
    if (container == null) {
      return;
    }

    container.clearAnimation();

    if (callBack == null) {
      callBack = new SimpleBitmapLoadCallBack<T>();
    }

    if (displayConfig == null || displayConfig == defaultDisplayConfig) {
      displayConfig = defaultDisplayConfig.cloneNew();
    }

    // Optimize Max Size
    BitmapSize size = displayConfig.getBitmapMaxSize();
    displayConfig.setBitmapMaxSize(
        BitmapCommonUtils.optimizeMaxSizeByView(container, size.getWidth(), size.getHeight()));

    callBack.onPreLoad(container, uri, displayConfig);

    if (TextUtils.isEmpty(uri)) {
      callBack.onLoadFailed(container, uri, displayConfig.getLoadFailedDrawable());
      return;
    }

    Bitmap bitmap = globalConfig.getBitmapCache().getBitmapFromMemCache(uri, displayConfig);

    if (bitmap != null) {
      callBack.onLoadStarted(container, uri, displayConfig);
      callBack.onLoadCompleted(container, uri, bitmap, displayConfig, BitmapLoadFrom.MEMORY_CACHE);
    } else if (!bitmapLoadTaskExist(container, uri, callBack)) {

      final BitmapLoadTask<T> loadTask =
          new BitmapLoadTask<T>(container, uri, displayConfig, callBack);
      // set loading image
      final AsyncDrawable<T> asyncDrawable =
          new AsyncDrawable<T>(displayConfig.getLoadingDrawable(), loadTask);
      callBack.setDrawable(container, asyncDrawable);

      // load bitmap from uri or diskCache
      loadTask.executeOnExecutor(globalConfig.getBitmapLoadExecutor());
    }
  }
예제 #5
0
  public void display(
      ImageView imageView, String uri, Bitmap loadingBitmap, CompressFormat compressFormat) {
    BitmapDisplayConfig displayConfig = displayConfigMap.get(String.valueOf(loadingBitmap));
    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
      displayConfig.setLoadingBitmap(loadingBitmap);
      displayConfigMap.put(String.valueOf(loadingBitmap), displayConfig);
    }

    doDisplay(imageView, uri, displayConfig, compressFormat);
  }
예제 #6
0
 private void addBitmapToMemoryCache(
     String uri, BitmapDisplayConfig config, Bitmap bitmap, long expiryTimestamp)
     throws IOException {
   if (uri != null
       && bitmap != null
       && globalConfig.isMemoryCacheEnabled()
       && mMemoryCache != null) {
     MemoryCacheKey key = new MemoryCacheKey(uri, config);
     mMemoryCache.put(key, bitmap, expiryTimestamp);
   }
 }
예제 #7
0
  /** Initialize the memory cache */
  public void initMemoryCache() {
    if (!globalConfig.isMemoryCacheEnabled()) return;

    // Set up memory cache
    if (mMemoryCache != null) {
      try {
        clearMemoryCache();
      } catch (Throwable e) {
      }
    }
    mMemoryCache =
        new LruMemoryCache<MemoryCacheKey, Bitmap>(globalConfig.getMemoryCacheSize()) {
          /**
           * Measure item size in bytes rather than units which is more practical for a bitmap cache
           */
          @Override
          protected int sizeOf(MemoryCacheKey key, Bitmap bitmap) {
            if (bitmap == null) return 0;
            return bitmap.getRowBytes() * bitmap.getHeight();
          }
        };
  }
예제 #8
0
  /**
   * Initializes the disk cache. Note that this includes disk access so this should not be executed
   * on the main/UI thread. By default an ImageCache does not initialize the disk cache when it is
   * created, instead you should call initDiskCache() to initialize it on a background thread.
   */
  public void initDiskCache() {
    if (!globalConfig.isDiskCacheEnabled()) return;

    // Set up disk cache
    synchronized (mDiskCacheLock) {
      if (mDiskLruCache == null || mDiskLruCache.isClosed()) {
        File diskCacheDir = new File(globalConfig.getDiskCachePath());
        if (diskCacheDir.exists() || diskCacheDir.mkdirs()) {
          long availableSpace = OtherUtils.getAvailableSpace(diskCacheDir);
          long diskCacheSize = globalConfig.getDiskCacheSize();
          diskCacheSize = availableSpace > diskCacheSize ? diskCacheSize : availableSpace;
          try {
            mDiskLruCache = LruDiskCache.open(diskCacheDir, 1, 1, diskCacheSize);
            mDiskLruCache.setFileNameGenerator(globalConfig.getFileNameGenerator());
          } catch (Throwable e) {
            mDiskLruCache = null;
            LogUtils.e(e.getMessage(), e);
          }
        }
      }
      isDiskCacheReadied = true;
      mDiskCacheLock.notifyAll();
    }
  }
예제 #9
0
  public void display(
      ImageView imageView,
      String uri,
      int imageWidth,
      int imageHeight,
      CompressFormat compressFormat) {
    BitmapDisplayConfig displayConfig = displayConfigMap.get(imageWidth + "_" + imageHeight);
    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
      displayConfig.setBitmapHeight(imageHeight);
      displayConfig.setBitmapWidth(imageWidth);
      displayConfigMap.put(imageWidth + "_" + imageHeight, displayConfig);
    }

    doDisplay(imageView, uri, displayConfig, compressFormat);
  }
예제 #10
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;
  }
예제 #11
0
  public void display(
      ImageView imageView,
      String uri,
      int imageWidth,
      int imageHeight,
      Bitmap loadingBitmap,
      Bitmap loadFailedBitmap,
      CompressFormat compressFormat) {
    BitmapDisplayConfig displayConfig =
        displayConfigMap.get(
            imageWidth
                + "_"
                + imageHeight
                + "_"
                + String.valueOf(loadingBitmap)
                + "_"
                + String.valueOf(loadFailedBitmap));
    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
      displayConfig.setBitmapHeight(imageHeight);
      displayConfig.setBitmapWidth(imageWidth);
      displayConfig.setLoadingBitmap(loadingBitmap);
      displayConfig.setLoadFailedBitmap(loadFailedBitmap);
      displayConfigMap.put(
          imageWidth
              + "_"
              + imageHeight
              + "_"
              + String.valueOf(loadingBitmap)
              + "_"
              + String.valueOf(loadFailedBitmap),
          displayConfig);
    }

    doDisplay(imageView, uri, displayConfig, compressFormat);
  }
예제 #12
0
 public BitmapUtils configDownloader(Downloader downloader) {
   globalConfig.setDownloader(downloader);
   return this;
 }
예제 #13
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;
  }
예제 #14
0
 public BitmapUtils configLoadFailedImage(int resId) {
   globalConfig
       .getDefaultDisplayConfig()
       .setLoadFailedBitmap(BitmapFactory.decodeResource(context.getResources(), resId));
   return this;
 }
예제 #15
0
 public Bitmap getBitmapFromMemCache(String uri) {
   return globalConfig.getBitmapCache().getBitmapFromMemCache(uri);
 }
예제 #16
0
 public void closeCache() {
   globalConfig.closeCache();
 }
예제 #17
0
 public void flushCache() {
   globalConfig.flushCache();
 }
예제 #18
0
 public BitmapUtils configDiskCacheEnabled(boolean enabled) {
   globalConfig.setDiskCacheEnabled(enabled);
   return this;
 }
예제 #19
0
 public void clearDiskCache() {
   globalConfig.clearDiskCache();
 }
예제 #20
0
 public BitmapUtils configBitmapMaxWidth(int bitmapWidth) {
   globalConfig.getDefaultDisplayConfig().setBitmapWidth(bitmapWidth);
   return this;
 }
예제 #21
0
 public BitmapUtils configImageLoadCallBack(ImageLoadCallBack imageLoadCallBack) {
   globalConfig.setImageLoadCallBack(imageLoadCallBack);
   return this;
 }
예제 #22
0
 public BitmapUtils(
     Context context, String diskCachePath, float memoryCachePercent, int diskCacheSize) {
   this(context, diskCachePath);
   globalConfig.setMemCacheSizePercent(memoryCachePercent);
   globalConfig.setDiskCacheSize(diskCacheSize);
 }
예제 #23
0
 public BitmapUtils configDefaultCompressFormat(CompressFormat format) {
   globalConfig.setDefaultCompressFormat(format);
   return this;
 }
예제 #24
0
 public BitmapUtils configThreadPoolSize(int poolSize) {
   globalConfig.setThreadPoolSize(poolSize);
   return this;
 }
예제 #25
0
 public BitmapUtils configLoadFailedImage(Bitmap bitmap) {
   globalConfig.getDefaultDisplayConfig().setLoadFailedBitmap(bitmap);
   return this;
 }
예제 #26
0
 public void clearDiskCache(String url) {
   globalConfig.clearDiskCache(url);
 }
예제 #27
0
 public void clearMemoryCache() {
   globalConfig.clearMemoryCache();
 }
예제 #28
0
 public BitmapUtils configCalculateBitmap(boolean neverCalculate) {
   globalConfig.getBitmapDownloadProcess().neverCalculate(neverCalculate);
   return this;
 }
예제 #29
0
 public void clearMemoryCache(String url) {
   globalConfig.clearMemoryCache(url);
 }
예제 #30
0
 public BitmapUtils configBitmapMaxHeight(int bitmapHeight) {
   globalConfig.getDefaultDisplayConfig().setBitmapHeight(bitmapHeight);
   return this;
 }