在内存中保存的话,只能保存一定的量,而不能一直往里面放,需要设置数据的过期时间、LRU等算法。这里有一个方法是把常用的数据放到一个缓存中(A),不常用的放到另外一个缓存中(B)。当要获取数据时先从A中去获取,如果A中不存在那么再去B中获取。B中的数据主要是ALRU出来的数据,这里的内存回收主要针对B内存,从而保持A中的数据可以有效的被命中。

先定义A缓存:

java代码:

private final HashMapmHardBitmapCache = new LinkedHashMap(HARD_CACHE_CAPACITY/ 2, 0.75f, true) {
@Override
protected booleanremoveEldestEntry(LinkedHashMap.Entry eldest) {
if (size() >HARD_CACHE_CAPACITY) {
//mapsize大于30时,把最近不常用的key放到mSoftBitmapCache中,从而保证mHardBitmapCache的效率
mSoftBitmapCache.put(eldest.getKey(), newSoftReference(eldest.getValue()));
return true;
} else
return false;
}
};
再定义B缓存:

java代码:

/**
*mHardBitmapCachekey大于30的时候,会根据LRU算法把最近没有被使用的key放入到这个缓存中。
*Bitmap使用了SoftReference,当内存空间不足时,此cache中的bitmap会被垃圾回收掉
*/
private final staticConcurrentHashMap> mSoftBitmapCache =new ConcurrentHashMap

从缓存中获取数据:

java代码:

/**
* 从缓存中获取图片
*/
private Bitmap getBitmapFromCache(Stringurl) {
// 先从mHardBitmapCache缓存中获取
synchronized (mHardBitmapCache) {
final Bitmap bitmap =mHardBitmapCache.get(url);
if (bitmap != null) {
//如果找到的话,把元素移到linkedhashmap的最前面,从而保证在LRU算法中是最后被删除
mHardBitmapCache.remove(url);
mHardBitmapCache.put(url,bitmap);
return bitmap;
}
}
//如果mHardBitmapCache中找不到,到mSoftBitmapCache中找

SoftReferencebitmapReference = mSoftBitmapCache.get(url);
if (bitmapReference != null) {
final Bitmap bitmap =bitmapReference.get();
if (bitmap != null) {
return bitmap;
} else {
mSoftBitmapCache.remove(url);
}
}
return null;
}
如果缓存中不存在,那么就只能去服务器端去下载:

java代码:

/**
* 异步下载图片
*/
class ImageDownloaderTask extendsAsyncTask {
private static final int IO_BUFFER_SIZE= 4 * 1024;
private String url;
private finalWeakReference p_w_picpathViewReference;
public ImageDownloaderTask(ImageViewp_w_picpathView) {
p_w_picpathViewReference = newWeakReference(p_w_picpathView);
}

@Override
protected BitmapdoInString... params) {
final AndroidHttpClient client =AndroidHttpClient.newInstance("Android");
url = params[0];
final HttpGet getRequest = newHttpGet(url);
try {
HttpResponse response =client.execute(getRequest);
final int statusCode =response.getStatusLine().getStatusCode();
if (statusCode !=HttpStatus.SC_OK) {
Log.w(TAG, "" +url + "中下载图片时出错!,错误码:" + statusCode);
return null;
}
final HttpEntity entity =response.getEntity();
if (entity != null) {
InputStream inputStream =null;
OutputStream outputStream =null;
try {
inputStream =entity.getContent();
finalByteArrayOutputStream dataStream = new ByteArrayOutputStream();
outputStream = newBufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(inputStream,outputStream);
outputStream.flush();
final byte[] data =dataStream.toByteArray();
final Bitmap bitmap =BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
} finally {
if (inputStream !=null) {

inputStream.close();
}
if (outputStream !=null) {
outputStream.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
getRequest.abort();
Log.w(TAG, "I/O errorwhile retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
getRequest.abort();
Log.w(TAG, "Incorrect URL:" + url);
} catch (Exception e) {
getRequest.abort();
Log.w(TAG, "Error whileretrieving bitmap from " + url, e);
} finally {
if (client != null) {
client.close();
}
}
return null;
}



转载自http://blog.csdn.net/snowleopard_wu/article/details/6858497

更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. 一句话锁定MySQL数据占用元凶
  3. Android(安卓)利用阿里UTD库 获取手机唯一标识
  4. Android中直播视频技术探究之---摄像头Camera视频源数据采集解析
  5. Android隐喻(三) 图形绘制: Canvas、SurfaceView、Paint、Surface
  6. Android实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼
  7. Android跳转系统界面_大全集
  8. 【移动开发】Android图片异步加载之Android-Universal-Image-Loa
  9. Android(安卓)UI总结 Android(安卓)和H5 字体大小适配

随机推荐

  1. Android(安卓)进度条 ProgressBar (模拟图
  2. Android学习过程
  3. Android百度地图——路径规划(驾车、步行
  4. Android内存泄露利器MLT(整合篇)
  5. Android标题栏TitleBar全攻略
  6. execlp启动android进程命令窗口通过adb s
  7. Android EditView属性详细介绍
  8. Android 面试题
  9. Ubuntu上开发Android
  10. Android - 开发者应该深入学习的10个开源