/** * get one image from three places, imageMemoryCache,imageFileCache,network * * @param url The URL of the image to download. * @return Bitmap return a bitmap */ private Bitmap getBitmap(final String url) { if (null == url || "".equals(url)) { return null; } // get image from mLruMemoryCache Bitmap result = null; result = mLruMemoryCache.get(url); if (result == null) { // get image from imageFileCache result = imageFileCache.getImage(url); if (result == null) { // get image from network // result = getImageHttp(url); result = getImageFromHttp(url); if (result != null) { if (mLruMemoryCache.get(url) == null) { mLruMemoryCache.put(url, result); } imageFileCache.addImgToSDTask(url, result); } } else { if (mLruMemoryCache.get(url) == null) { mLruMemoryCache.put(url, result); } } } return result; }
/** * Download the specified image from the Internet and binds it to the provided ImageView. The * binding is immediate if the image is found in the cache and will be done asynchronously * otherwise. A null bitmap will be associated to the ImageView if an error occurs. * * @param url The URL of the image to download. * @param imageView The ImageView to bind the downloaded image to. */ @Deprecated public void download(BindHolder holder, Callback callback) { String url = holder.getUrl(); if (null == url || "".equals(url)) { return; } // TO DO : sbh : don't store bitmap object in cache , use local files (compressed cache) // Bitmap bitmap = imageMemoryCache.getBitmapFromCache(url); Bitmap bitmap = mLruMemoryCache.get(url); if (bitmap == null) { executorService.submit(new ImageTask(new ImageTaskHandler(callback), url, holder)); } else { Logger.d(" return image from the cache, " + bitmap + " from url =" + url); holder.setResource(bitmap); callback.callback(holder); } }
/** * used for ImageView to download images. * * <p>if you use "ViewHolder" and "convertView.getTag()" in listView or gridView, you must use * this method'. * * <p>for image, if you just use bitmap from this method, you don't need to recycle this bitmap. * if you create a temporary bitmap, you must recycle it by yourself. * * @param url * @param defaultBitmap * @param imageView */ public void download(String url, Bitmap defaultBitmap, ImageView imageView) { if (null == url || "".equals(url) || "null".equals(url)) { return; } Bitmap bitmap = null; // bitmap = imageMemoryCache.getBitmapFromSoftCache(url); // bitmap = memoryCache.get(url); bitmap = mLruMemoryCache.get(url); if (bitmap != null) { // Logger.debug(this,"get image from memory cache, " + bitmap + " from url =" + url); imageView.setImageBitmap(bitmap); } else if (checkImageTask(url, imageView)) { final BitmapLoadAndDisplayTask task = new BitmapLoadAndDisplayTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.getResources(), defaultBitmap, task); imageView.setImageDrawable(asyncDrawable); task.executeOnExecutor(executorService, url); } }
/** 清理内存图片 */ public void clearMemoryCache() { memoryCache.clear(); }
@Override public void remove(String key) { mMemoryCache.remove(key); }
@Override public void evictAll() { mMemoryCache.evictAll(); }
@Override public Bitmap get(String key) { return mMemoryCache.get(key); }
@Override public void put(String key, Bitmap bitmap) { mMemoryCache.put(key, bitmap); }
/** release the memory of mLruMemoryCache */ public void release() { mLruMemoryCache.clearCache(); }