import java.io.File;import java.lang.ref.SoftReference;import java.util.HashMap;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Message;import android.widget.ImageView;import android.widget.ProgressBar;public class AsyncBitmapLoader {/** * 内存图片软引用缓冲 */private HashMap<String, SoftReference<Bitmap>> imageCache = null;private String localImagePath = "";public AsyncBitmapLoader(String localImagePath) {imageCache = new HashMap<String, SoftReference<Bitmap>>();this.localImagePath = localImagePath;}/** * @param imageView * @param progressBar * @param imageURL * @param imageCallBack * @param position *            list中第几项 * @param size *            设定图片导出大小 * @param fromAct *            "main"来自主界面, "detail"来自详情 * @return */public Bitmap loadBitmap(final ImageView imageView, final ProgressBar progressBar, final String imageURL, final ImageCallBack imageCallBack,final int position, final int size, final String fromAct) {final String picName = imageURL.substring(imageURL.lastIndexOf("/") + 1);// 在内存缓存中,则返回Bitmap对象Bitmap bitmap = null;if (imageCache.containsKey(imageURL)) {SoftReference<Bitmap> reference = imageCache.get(imageURL);bitmap = reference.get();if (bitmap != null) {return bitmap;}}/** * 加上一个对本地缓存的查找 */String bitmapName = picName;File cacheDir = new File(localImagePath);File[] cacheFiles = cacheDir.listFiles();int i = 0;for (; i < cacheFiles.length; i++) {if (bitmapName.equals(cacheFiles[i].getName())) {break;}}final Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubif (msg.what == 0)imageCallBack.imageLoad(imageView, progressBar, (Bitmap) msg.obj, msg.arg1);else if (msg.what == 1) {imageCallBack.imageLoad(imageView, progressBar, msg.arg1, msg.arg2);// System.out.println(msg.arg1);}}};if (i < cacheFiles.length) {new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubBitmap bitmap = null;BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;bitmap = BitmapFactory.decodeFile(localImagePath + picName, options); // 此时返回bm为空options.inJustDecodeBounds = false;int be = (int) (options.outHeight / (float) size);if (be <= 0)be = 1;if (fromAct.equals("detail")) {options.inSampleSize = be;} else {options.inSampleSize = be;options.inPurgeable = true;options.inInputShareable = true;options.inPreferredConfig = Bitmap.Config.ARGB_4444;}bitmap = BitmapFactory.decodeFile(localImagePath + picName, options);imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));Message msg2 = handler.obtainMessage(0, position, position, bitmap);// 把自身位置传进去handler.sendMessage(msg2);}}).start();return null;}// 如果不在内存缓存中,也不在本地(被jvm回收掉),则开启线程下载图片new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubBitmap bitmap = null;int i = 0;boolean downOk = false;handler.post(new Runnable() {@Overridepublic void run() {Message msg = handler.obtainMessage(1, 0, position);handler.sendMessage(msg);}});// 下载while (i < 3) {if (MyHttpURLConnection_Util.downloadFile(imageURL, localImagePath + picName, null, new ProgressControl() {@Overridepublic void messageControl(String str) {}@Overridepublic void stepControl(final double j) {// TODO Auto-generated method stubMessage msg = handler.obtainMessage(1, (int) j, position);handler.sendMessage(msg);}})) {downOk = true;break;}i++;}if (!downOk) {System.out.println("downFailed--" + imageURL);// Message msg1 = handler.obtainMessage(0, bitmap);// handler.sendMessage(msg1);return;}// 解码缩放BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;bitmap = BitmapFactory.decodeFile(localImagePath + picName, options); // 此时返回bm为空options.inJustDecodeBounds = false;int be = (int) (options.outHeight / (float) size);if (be <= 0)be = 1;if (fromAct.equals("detail")) {options.inSampleSize = be;} else {options.inSampleSize = be;options.inPurgeable = true;options.inInputShareable = true;options.inPreferredConfig = Bitmap.Config.ARGB_4444;}bitmap = BitmapFactory.decodeFile(localImagePath + picName, options);// 放入缓存imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));Message msg2 = handler.obtainMessage(0, position, position, bitmap);// 把自身位置传进去handler.sendMessage(msg2);}}.start();return null;}}

更多相关文章

  1. android ion 内存泄漏排查
  2. 【Android外文翻译 - 01】避免内存泄漏
  3. android WebView 回退与退出
  4. 缓存工具类封装
  5. Android(安卓)的网络编程(5)-通过ksoap2调用webservice示例
  6. android客户程序调用Webservice 心得
  7. Android(安卓)缓存的工具类
  8. Android中如何判断内存卡是否存在
  9. Android:管理应用内存

随机推荐

  1. Android2.2 API 中文文档系列(9) ―― Zoom
  2. 系出名门Android(2) - 布局(Layout)和菜
  3. 什么是 Android?
  4. 创建第一个Andorid程序
  5. Android操作HTTP实现与服务器通信
  6. Android应用程序与SurfaceFlinger服务的
  7. android 移植 一 准备
  8. android中文字高亮设置案例
  9. Appium - Android(安卓)对比 iOS
  10. Android客户端通过socket与服务器通信