自动判断从内存还是网络获取图片,缓存自动回收
import java.io.InputStream;import java.lang.ref.SoftReference;import java.net.HttpURLConnection;import java.net.URL;import java.util.Date;import java.util.HashMap;import android.graphics.Bitmap;import android.graphics.BitmapFactory;/** *  * @author yizhe * @date 2012-6-13 */public class BitmapLoader {// 内存缓存,SoftReference实现自动回收private static HashMap<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();/** * 自动判断从内存还是从网络获取图片 *  * @param imageURL * @return */public static Bitmap loadBitmap(String imageURL) {long time = new Date().getTime();Comm.print(time + "--loadBitmap--" + imageURL);Bitmap bm = null;if (imageCache.containsKey(imageURL)) {// 从内存中获取Comm.print(time + "--loadBitmap--from Cache");SoftReference<Bitmap> reference = imageCache.get(imageURL);bm = reference.get();}if (null == bm) {// 到网络下载Comm.print(time + "--loadBitmap--from Net");bm = getNetBitmap2(imageURL);if (null != bm) {imageCache.put(imageURL, new SoftReference<Bitmap>(bm)); // 保存到内存}}if (null == bm) {Comm.print(time + "--loadBitmap--false");} else {Comm.print(time + "--loadBitmap--true");}return bm;}// 从网络下载图片,方法2,本方法下载图片可能出现返回200状态但得到的bitmap为null的情况private static Bitmap getNetBitmap(String imageURL) {Bitmap bitmap = null;try {// 要下载的图片的地址,URL url = new URL(imageURL);// 获取到路径HttpURLConnection conn = (HttpURLConnection) url.openConnection();// http协议连接对象conn.setRequestMethod("GET");// 这里是不能乱写的,详看API方法conn.setConnectTimeout(9000);// 别超过10秒。int state = conn.getResponseCode();if (state == 200) {Comm.print("--loadNetBitmap--" + state);InputStream inputStream = conn.getInputStream();bitmap = BitmapFactory.decodeStream(inputStream);}} catch (Exception e) {Comm.print("--loadNetBitmap--e--" + e.toString());}return bitmap;}// 从网络下载图片,方法2,请使用本方法下载图片,public static Bitmap getNetBitmap2(String imageURL) {Bitmap bitmap = null;try {URL url = new URL(imageURL);// 获取到路径HttpURLConnection conn = (HttpURLConnection) url.openConnection();// http协议连接对象conn.setRequestMethod("GET");// 这里是不能乱写的,详看API方法conn.setConnectTimeout(9000);// 别超过10秒。int state = conn.getResponseCode();if (state == 200) {Comm.print("--loadNetBitmap--" + state);int length = (int) conn.getContentLength();// 获取长度InputStream is = conn.getInputStream();if (length != -1) {byte[] imgData = new byte[length];byte[] temp = new byte[512];int readLen = 0;int destPos = 0;while ((readLen = is.read(temp)) > 0) {System.arraycopy(temp, 0, imgData, destPos, readLen);destPos += readLen;}bitmap = BitmapFactory.decodeByteArray(imgData, 0,imgData.length);}}} catch (Exception e) {Comm.print("--loadNetBitmap--e--" + e.toString());}return bitmap;}}

更多相关文章

  1. Android(安卓)inject input events 注入Touch 点(x, y) 触摸输入
  2. Android验证码倒计时功能实现
  3. Android(安卓)SearchView结合Filter实现列表检索功能使用总结
  4. Android获取屏幕宽高的方法
  5. [Android]RIL的启动流程
  6. android如何获取SD卡上的多媒体文件
  7. 浅谈Java中Collections.sort对List排序的两种方法
  8. Python list sort方法的具体使用
  9. python list.sort()根据多个关键字排序的方法实现

随机推荐

  1. Flex布局实现头尾固定、中间内容自适应
  2. Linux运维必会的100道MySql面试题之(三)
  3. Linux基本权限类别
  4. CSS3选择器 :nth-child()的用法
  5. Linux系统集群架构线上项目配置实战(一)
  6. CDN加速有用吗?对网站有什么好处?
  7. 优盘出现“损坏打不开”的解决办法
  8. css第一课
  9. css基础、选择器、模块化组件
  10. Oracle OCP 071中文考试题库-第14题