/** * @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); } }); } }); } }
@Override public void run() { try { synchronized (imageCache) { mCallback.onStart(url); } Bitmap bitmap = null; if (url != null && !"".equals(url)) { byte[] b = getUrlBytes(url); /* * BitmapFactory.Options opts = new * BitmapFactory.Options(); opts.inJustDecodeBounds * = true; * BitmapFactory.decodeStream(getUrlInputStream * (imgUrl), null, opts); * * opts.inSampleSize = computeSampleSize(opts, -1, * 128*128); opts.inJustDecodeBounds = false; try { * bitmap = * BitmapFactory.decodeStream(getUrlInputStream * (imgUrl), null, opts); } catch (OutOfMemoryError * err) { } */ bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); // Bitmap bitmap = BitmapFactory.decodeStream(bis); } Message msg = mHandler.obtainMessage(); Map<String, Bitmap> bitmapMap = new HashMap<String, Bitmap>(); bitmapMap.put(url, bitmap); msg.obj = bitmapMap; mHandler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); synchronized (imageCache) { mCallback.onFailed(url); } } }
public static Drawable loadDrawable( final String imageUrl, final ImageCallback imageCallback, final Context activity) { try { // System.out.println(imageUrl); Drawable drawable = loadImageFromCache(imageUrl); if (drawable != null) { Log.d(TAG, "Cache Hit:" + imageUrl); imageCallback.imageLoaded(drawable, imageUrl); return drawable; } Log.d(TAG, "Cache Miss:" + imageUrl); final Handler handler = new Handler() { @Override public void handleMessage(Message message) { if (imageCallback != null) imageCallback.imageLoaded((Drawable) message.obj, imageUrl); } }; queue.put( new Runnable() { public void run() { Log.d(TAG, "URL loading:" + imageUrl); Drawable drawable = loadImageFromUrl(imageUrl, activity); Log.d(TAG, "URL Hit:" + imageUrl + ", drawable=" + drawable); Message message = handler.obtainMessage(0, drawable); handler.sendMessage(message); } }); beep(); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * @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); } }); } }); } }
public Bitmap loadImage( final String imageName, final String imgUrl, final boolean isbusy, final ImageCallback callback) { Bitmap bitmap = null; if (imageCache.containsKey(imgUrl)) { SoftReference<Bitmap> softReference = imageCache.get(imgUrl); bitmap = softReference.get(); if (bitmap != null) { Log.i("m", "从内存获得图片成功。。"); callback.loadImage(bitmap, imgUrl); return bitmap; } } // if(!isbusy){ // 从本地获取图片 bitmap = getBitmapFromData(imageName, mContext); // System.out.println("从本地获取图片"); // } if (bitmap != null) { imageCache.put(imgUrl, new SoftReference<Bitmap>(bitmap)); Log.i("m", "从本地获得图片成功。。"); callback.loadImage(bitmap, imgUrl); return bitmap; } else { // 从网络获取图片 final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.obj != null) { synchronized (imageCache) { Map<String, Bitmap> bitmapMap = (Map) msg.obj; for (String str : bitmapMap.keySet()) { mbitmap = bitmapMap.get(str); url = str; imageCache.put(url, new SoftReference<Bitmap>(mbitmap)); } if (android.os.Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED)) { try { String imageName = url.substring(url.lastIndexOf("/") + 1, url.length()); if (!imageName.endsWith(".jpg") && !imageName.endsWith(".png")) { imageName += ".png"; } String mImagePath = ImageUtil.IMAGE_PATH + imageName; /*if (url.endsWith(".png")) { saveImagePng(mImagePath, mbitmap); } else if (url.endsWith(".jpg")) { saveImageJpeg(mImagePath, mbitmap); }*/ saveBitmapToData(imageName, mbitmap, mContext); // System.out.println("向SD卡获取图片"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } callback.loadImage(mbitmap, url); } } } }; getImageRunnable run = new getImageRunnable(imgUrl, handler, callback); ThreadPoolManager.getInstance().addTask(run); } return bitmap; }