Android 遠程圖片擷取和本機快取(三)

來源:互聯網
上載者:User

   從緩衝中擷取資料:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.