歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之圖片異步加載並緩存到本地

Android之圖片異步加載並緩存到本地

日期:2017/3/1 10:20:15   编辑:Linux編程

在Android項目中訪問網絡圖片是非常普遍性的事情,如果我們每次請求都要訪問網絡來獲取圖片,會非常耗費流量,而且圖片占用內存空間也比較大,圖片過多且不釋放的話很容易造成內存溢出。針對上面遇到的兩個問題,首先耗費流量我們可以將圖片第一次加載上面緩存到本地,以後如果本地有就直接從本地加載。圖片過多造成內存溢出,這個是最不容易解決的,要想一些好的緩存策略,比如大圖片使用LRU緩存策略或懶加載緩存策略。今天首先介紹一下本地緩存圖片。

首先看一下異步加載緩存本地代碼:

  1. public class AsyncBitmapLoader
  2. {
  3. /**
  4. * 內存圖片軟引用緩沖
  5. */
  6. private HashMap<String, SoftReference<Bitmap>> imageCache = null;
  7. public AsyncBitmapLoader()
  8. {
  9. imageCache = new HashMap<String, SoftReference<Bitmap>>();
  10. }
  11. public Bitmap loadBitmap(final ImageView imageView, final String imageURL, final ImageCallBack imageCallBack)
  12. {
  13. //在內存緩存中,則返回Bitmap對象
  14. if(imageCache.containsKey(imageURL))
  15. {
  16. SoftReference<Bitmap> reference = imageCache.get(imageURL);
  17. Bitmap bitmap = reference.get();
  18. if(bitmap != null)
  19. {
  20. return bitmap;
  21. }
  22. }
  23. else
  24. {
  25. /**
  26. * 加上一個對本地緩存的查找
  27. */
  28. String bitmapName = imageURL.substring(imageURL.lastIndexOf("/") + 1);
  29. File cacheDir = new File("/mnt/sdcard/test/");
  30. File[] cacheFiles = cacheDir.listFiles();
  31. int i = 0;
  32. if(null!=cacheFiles){
  33. for(; i<cacheFiles.length; i++)
  34. {
  35. if(bitmapName.equals(cacheFiles[i].getName()))
  36. {
  37. break;
  38. }
  39. }
  40. if(i < cacheFiles.length)
  41. {
  42. return BitmapFactory.decodeFile("/mnt/sdcard/test/" + bitmapName);
  43. }
  44. }
  45. }
  46. final Handler handler = new Handler()
  47. {
  48. /* (non-Javadoc)
  49. * @see android.os.Handler#handleMessage(android.os.Message)
  50. */
  51. @Override
  52. public void handleMessage(Message msg)
  53. {
  54. // TODO Auto-generated method stub
  55. imageCallBack.imageLoad(imageView, (Bitmap)msg.obj);
  56. }
  57. };
  58. //如果不在內存緩存中,也不在本地(被jvm回收掉),則開啟線程下載圖片
  59. new Thread()
  60. {
  61. /* (non-Javadoc)
  62. * @see java.lang.Thread#run()
  63. */
  64. @Override
  65. public void run()
  66. {
  67. // TODO Auto-generated method stub
  68. InputStream bitmapIs = HttpUtils.getStreamFromURL(imageURL);
  69. Bitmap bitmap = BitmapFactory.decodeStream(bitmapIs);
  70. imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));
  71. Message msg = handler.obtainMessage(0, bitmap);
  72. handler.sendMessage(msg);
  73. File dir = new File("/mnt/sdcard/test/");
  74. if(!dir.exists())
  75. {
  76. dir.mkdirs();
  77. }
  78. File bitmapFile = new File("/mnt/sdcard/test/" +
  79. imageURL.substring(imageURL.lastIndexOf("/") + 1));
  80. if(!bitmapFile.exists())
  81. {
  82. try
  83. {
  84. bitmapFile.createNewFile();
  85. }
  86. catch (IOException e)
  87. {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. }
  91. }
  92. FileOutputStream fos;
  93. try
  94. {
  95. fos = new FileOutputStream(bitmapFile);
  96. bitmap.compress(Bitmap.CompressFormat.PNG,
  97. 100, fos);
  98. fos.close();
  99. }
  100. catch (FileNotFoundException e)
  101. {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105. catch (IOException e)
  106. {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. }
  110. }
  111. }.start();
  112. return null;
  113. }
  114. public interface ImageCallBack
  115. {
  116. public void imageLoad(ImageView imageView, Bitmap bitmap);
  117. }
  118. }
Copyright © Linux教程網 All Rights Reserved