private Bitmap decodeBitmapMeta(BitmapMeta bitmapMeta, BitmapDisplayConfig config) throws IOException { if (bitmapMeta == null) return null; Bitmap bitmap = null; if (bitmapMeta.inputStream != null) { if (config == null || config.isShowOriginal()) { bitmap = BitmapDecoder.decodeFileDescriptor(bitmapMeta.inputStream.getFD()); } else { bitmap = BitmapDecoder.decodeSampledBitmapFromDescriptor( bitmapMeta.inputStream.getFD(), config.getBitmapMaxSize(), config.getBitmapConfig()); } } else if (bitmapMeta.data != null) { if (config == null || config.isShowOriginal()) { bitmap = BitmapDecoder.decodeByteArray(bitmapMeta.data); } else { bitmap = BitmapDecoder.decodeSampledBitmapFromByteArray( bitmapMeta.data, config.getBitmapMaxSize(), config.getBitmapConfig()); } } return bitmap; }
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()); } }
/** * 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; }