Android 远程图片获取和本地缓存(三)

   从缓存中获取数据:

java代码:

  1. /**
  2. * 从缓存中获取图片
  3. */
  4. private Bitmap getBitmapFromCache(Stringurl) {
  5. // 先从mHardBitmapCache缓存中获取
  6. synchronized (mHardBitmapCache) {
  7. final Bitmap bitmap =mHardBitmapCache.get(url);
  8. if (bitmap != null) {
  9. //如果找到的话,把元素移到linkedhashmap的最前面,从而保证在LRU算法中是最后被删除
  10. mHardBitmapCache.remove(url);
  11. mHardBitmapCache.put(url,bitmap);
  12. return bitmap;
  13. }
  14. }
  15. //如果mHardBitmapCache中找不到,到mSoftBitmapCache中找
  16. SoftReference<Bitmap>bitmapReference = mSoftBitmapCache.get(url);
  17. if (bitmapReference != null) {
  18. final Bitmap bitmap =bitmapReference.get();
  19. if (bitmap != null) {
  20. return bitmap;
  21. } else {
  22. mSoftBitmapCache.remove(url);
  23. }
  24. }
  25. return null;
  26. }
复制代码


       如果缓存中不存在,那么就只能去服务器端去下载:

java代码:

  1. /**
  2. * 异步下载图片
  3. */
  4. class ImageDownloaderTask extendsAsyncTask<String, Void, Bitmap> {
  5. private static final int IO_BUFFER_SIZE= 4 * 1024;
  6. private String url;
  7. private finalWeakReference<ImageView> imageViewReference;
  8. public ImageDownloaderTask(ImageViewimageView) {
  9. imageViewReference = newWeakReference<ImageView>(imageView);
  10. }
  11. @Override
  12. protected BitmapdoInBackground(String... params) {
  13. final AndroidHttpClient client =AndroidHttpClient.newInstance("Android");
  14. url = params[0];
  15. final HttpGet getRequest = newHttpGet(url);
  16. try {
  17. HttpResponse response =client.execute(getRequest);
  18. final int statusCode =response.getStatusLine().getStatusCode();
  19. if (statusCode !=HttpStatus.SC_OK) {
  20. Log.w(TAG, "从" +url + "中下载图片时出错!,错误码:" + statusCode);
  21. return null;
  22. }
  23. final HttpEntity entity =response.getEntity();
  24. if (entity != null) {
  25. InputStream inputStream =null;
  26. OutputStream outputStream =null;
  27. try {
  28. inputStream =entity.getContent();
  29. finalByteArrayOutputStream dataStream = new ByteArrayOutputStream();
  30. outputStream = newBufferedOutputStream(dataStream, IO_BUFFER_SIZE);
  31. copy(inputStream,outputStream);
  32. outputStream.flush();
  33. final byte[] data =dataStream.toByteArray();
  34. final Bitmap bitmap =BitmapFactory.decodeByteArray(data, 0, data.length);
  35. return bitmap;
  36. } finally {
  37. if (inputStream !=null) {
  38. inputStream.close();
  39. }
  40. if (outputStream !=null) {
  41. outputStream.close();
  42. }
  43. entity.consumeContent();
  44. }
  45. }
  46. } catch (IOException e) {
  47. getRequest.abort();
  48. Log.w(TAG, "I/O errorwhile retrieving bitmap from " + url, e);
  49. } catch (IllegalStateException e) {
  50. getRequest.abort();
  51. Log.w(TAG, "Incorrect URL:" + url);
  52. } catch (Exception e) {
  53. getRequest.abort();
  54. Log.w(TAG, "Error whileretrieving bitmap from " + url, e);
  55. } finally {
  56. if (client != null) {
  57. client.close();
  58. }
  59. }
  60. return null;
  61. }
复制代码


系列之Android 远程图片获取和本地缓存(一)的帖子链接http://www.eoeandroid.com/thread-98446-1-1.html
系列之Android 远程图片获取和本地缓存(二)的帖子链接http://www.eoeandroid.com/thread-98449-1-1.html

posted @ 2011-12-15 14:09  灰太狼_lilongmin  阅读(1048)  评论(0编辑  收藏  举报