Android 远程图片获取和本地缓存(三)
2011-12-15 14:09
585 查看
从缓存中获取数据:
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> imageViewReference;
public ImageDownloaderTask(ImageViewimageView) {
imageViewReference = newWeakReference<ImageView>(imageView);
}
@Override
protected BitmapdoInBackground(String... 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;
}
复制代码
系列之Android 远程图片获取和本地缓存(一)的帖子链接http://www.eoeandroid.com/thread-98446-1-1.html
系列之Android 远程图片获取和本地缓存(二)的帖子链接http://www.eoeandroid.com/thread-98449-1-1.html
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> imageViewReference;
public ImageDownloaderTask(ImageViewimageView) {
imageViewReference = newWeakReference<ImageView>(imageView);
}
@Override
protected BitmapdoInBackground(String... 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;
}
复制代码
系列之Android 远程图片获取和本地缓存(一)的帖子链接http://www.eoeandroid.com/thread-98446-1-1.html
系列之Android 远程图片获取和本地缓存(二)的帖子链接http://www.eoeandroid.com/thread-98449-1-1.html
相关文章推荐
- Android 远程图片获取及本地缓存
- Android本地缓存和远程图片获取的实现
- Android 之 远程图片获取和本地缓存
- Android之远程图片获取与本地缓存
- Android远程图片获取和本地缓存
- Android远程图片获取和本地缓存
- Android 之 远程图片获取和本地缓存
- Android 之 远程图片获取和本地缓存
- Android 之 远程图片获取和本地缓存
- Android 之 远程图片获取和本地缓存
- Android远程图片获取和本地缓存
- Android远程图片获取和本地缓存
- Android之远程图片获取和本地缓存
- Android本地缓存和远程图片获取的实现
- Android远程图片获取和本地缓存
- Android远程图片获取和本地缓存
- Android 获取远程图片与本地图片缓存
- Android 之 远程图片获取和本地缓存
- android-远程图片获取和本地缓存
- Android远程图片获取和本地缓存