Android 圖片下載本地內存的緩存方式

在內存中保存的話,只能保存一定的量,而不能一直往裏面放,需要設置數據的過期時間、LRU等算法。這裏有一個方法是把常用的數據放到一個緩存中(A),不常用的放到另外一個緩存中(B)。當要獲取數據時先從A中去獲取,如果A中不存在那麼再去B中獲取。B中的數據主要是ALRU出來的數據,這裏的內存回收主要針對B內存,從而保持A中的數據可以有效的被命中。
0_1318217302j7WS.gif

先定義A緩存:

java代碼:

private final HashMap<String, Bitmap>mHardBitmapCache = new LinkedHashMap<String, Bitmap>(HARD_CACHE_CAPACITY/ 2, 0.75f, true) {
@Override
protected booleanremoveEldestEntry(LinkedHashMap.Entry<String, Bitmap> eldest) {
if (size() >HARD_CACHE_CAPACITY) {
//mapsize大於30時,把最近不常用的key放到mSoftBitmapCache中,從而保證mHardBitmapCache的效率
mSoftBitmapCache.put(eldest.getKey(), newSoftReference<Bitmap>(eldest.getValue()));
return true;
} else
return false;
}
};
再定義B緩存:

java代碼:

/**
*mHardBitmapCachekey大於30的時候,會根據LRU算法把最近沒有被使用的key放入到這個緩存中。
*Bitmap使用了SoftReference,當內存空間不足時,此cache中的bitmap會被垃圾回收掉
*/
private final staticConcurrentHashMap<String, SoftReference<Bitmap>> 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中找

SoftReference<Bitmap>bitmapReference = 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<String, Void, Bitmap> {
private static final int IO_BUFFER_SIZE= 4 * 1024;
private String url;
private finalWeakReference<ImageView> p_w_picpathViewReference;
public ImageDownloaderTask(ImageViewp_w_picpathView) {
p_w_picpathViewReference = newWeakReference<ImageView>(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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章