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); } }
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; }
/** * 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; }
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()); } }
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); }
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); } }
/** 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(); } }; }
/** * 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(); } }
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); }
/** * 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; }
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); }
public BitmapUtils configDownloader(Downloader downloader) { globalConfig.setDownloader(downloader); return this; }
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; }
public BitmapUtils configLoadFailedImage(int resId) { globalConfig .getDefaultDisplayConfig() .setLoadFailedBitmap(BitmapFactory.decodeResource(context.getResources(), resId)); return this; }
public Bitmap getBitmapFromMemCache(String uri) { return globalConfig.getBitmapCache().getBitmapFromMemCache(uri); }
public void closeCache() { globalConfig.closeCache(); }
public void flushCache() { globalConfig.flushCache(); }
public BitmapUtils configDiskCacheEnabled(boolean enabled) { globalConfig.setDiskCacheEnabled(enabled); return this; }
public void clearDiskCache() { globalConfig.clearDiskCache(); }
public BitmapUtils configBitmapMaxWidth(int bitmapWidth) { globalConfig.getDefaultDisplayConfig().setBitmapWidth(bitmapWidth); return this; }
public BitmapUtils configImageLoadCallBack(ImageLoadCallBack imageLoadCallBack) { globalConfig.setImageLoadCallBack(imageLoadCallBack); return this; }
public BitmapUtils( Context context, String diskCachePath, float memoryCachePercent, int diskCacheSize) { this(context, diskCachePath); globalConfig.setMemCacheSizePercent(memoryCachePercent); globalConfig.setDiskCacheSize(diskCacheSize); }
public BitmapUtils configDefaultCompressFormat(CompressFormat format) { globalConfig.setDefaultCompressFormat(format); return this; }
public BitmapUtils configThreadPoolSize(int poolSize) { globalConfig.setThreadPoolSize(poolSize); return this; }
public BitmapUtils configLoadFailedImage(Bitmap bitmap) { globalConfig.getDefaultDisplayConfig().setLoadFailedBitmap(bitmap); return this; }
public void clearDiskCache(String url) { globalConfig.clearDiskCache(url); }
public void clearMemoryCache() { globalConfig.clearMemoryCache(); }
public BitmapUtils configCalculateBitmap(boolean neverCalculate) { globalConfig.getBitmapDownloadProcess().neverCalculate(neverCalculate); return this; }
public void clearMemoryCache(String url) { globalConfig.clearMemoryCache(url); }
public BitmapUtils configBitmapMaxHeight(int bitmapHeight) { globalConfig.getDefaultDisplayConfig().setBitmapHeight(bitmapHeight); return this; }