`
dowhathowtodo
  • 浏览: 771432 次
文章分类
社区版块
存档分类
最新评论

Android 图片下载本地内存的缓存方式

 
阅读更多

Android图片下载本地内存的缓存方式

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

先定义A缓存:

java代码:

privatefinalHashMap<String,Bitmap>mHardBitmapCache=newLinkedHashMap<String,Bitmap>(HARD_CACHE_CAPACITY/2,0.75f,true){
@Override
protectedbooleanremoveEldestEntry(LinkedHashMap.Entry<String,Bitmap>eldest){
if(size()>HARD_CACHE_CAPACITY){
//mapsize大于30时,把最近不常用的key放到mSoftBitmapCache中,从而保证mHardBitmapCache的效率
mSoftBitmapCache.put(eldest.getKey(),newSoftReference<Bitmap>(eldest.getValue()));
returntrue;
}else
returnfalse;
}
};
再定义B缓存:

java代码:

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

从缓存中获取数据:

java代码:

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

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

java代码:

/**
*异步下载图片
*/
classImageDownloaderTaskextendsAsyncTask<String,Void,Bitmap>{
privatestaticfinalintIO_BUFFER_SIZE=4*1024;
privateStringurl;
privatefinalWeakReference<ImageView>imageViewReference;
publicImageDownloaderTask(ImageViewimageView){
imageViewReference=newWeakReference<ImageView>(imageView);
}

@Override
protectedBitmapdoInBackground(String...params){
finalAndroidHttpClientclient=AndroidHttpClient.newInstance("Android");
url=params[0];
finalHttpGetgetRequest=newHttpGet(url);
try{
HttpResponseresponse=client.execute(getRequest);
finalintstatusCode=response.getStatusLine().getStatusCode();
if(statusCode!=HttpStatus.SC_OK){
Log.w(TAG,""+url+"中下载图片时出错!,错误码:"+statusCode);
returnnull;
}
finalHttpEntityentity=response.getEntity();
if(entity!=null){
InputStreaminputStream=null;
OutputStreamoutputStream=null;
try{
inputStream=entity.getContent();
finalByteArrayOutputStreamdataStream=newByteArrayOutputStream();
outputStream=newBufferedOutputStream(dataStream,IO_BUFFER_SIZE);
copy(inputStream,outputStream);
outputStream.flush();
finalbyte[]data=dataStream.toByteArray();
finalBitmapbitmap=BitmapFactory.decodeByteArray(data,0,data.length);
returnbitmap;
}finally{
if(inputStream!=null){

inputStream.close();
}
if(outputStream!=null){
outputStream.close();
}
entity.consumeContent();
}
}
}catch(IOExceptione){
getRequest.abort();
Log.w(TAG,"I/Oerrorwhileretrievingbitmapfrom"+url,e);
}catch(IllegalStateExceptione){
getRequest.abort();
Log.w(TAG,"IncorrectURL:"+url);
}catch(Exceptione){
getRequest.abort();
Log.w(TAG,"Errorwhileretrievingbitmapfrom"+url,e);
}finally{
if(client!=null){
client.close();
}
}
returnnull;
}

主要信息来源 移动开发者联盟

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics