WEB开发网
开发学院手机开发Android 开发 Android远程图片获取和本地缓存 阅读

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

 2012-07-15 11:42:04 来源:WEB开发网   
核心提示:先定义A缓存: private final HashMap<String, Bitmap>mHardBitmapCache = new LinkedHashMap<String, Bitmap>(HARD_CACHE_CAPACITY/ 2, 0.75f, true) { @Over
先定义A缓存:

  1. private final HashMap<String, Bitmap>mHardBitmapCache = new LinkedHashMap<String, Bitmap>(HARD_CACHE_CAPACITY/ 2, 0.75f, true) {
  2. @Override
  3. protected booleanremoveEldestEntry(LinkedHashMap.Entry<String, Bitmap> eldest) {
  4. if (size() >HARD_CACHE_CAPACITY) {
  5. //当map的size大于30时,把最近不常用的key放到mSoftBitmapCache中,从而保证mHardBitmapCache的效率
  6. mSoftBitmapCache.put(eldest.getKey(), newSoftReference<Bitmap>(eldest.getValue()));
  7. return true;
  8. } else
  9. return false;
  10. }
  11. };

再定于B缓存:

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

从缓存中获取数据:

  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. }

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

  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. }

这是两种做法,还有一些应用在下载的时候使用了线程池和消息队列MQ,对于图片下载的效率要更好一些。有兴趣的同学可以看下。

上一页  1 2 3 

Tags:Android 远程 图片

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接