1. 程式人生 > >Android快取和讀取快取,網路載入圖片

Android快取和讀取快取,網路載入圖片

在開發中,遇到很多大量圖片呈現給使用者,那麼我們怎麼又快又高效的顯示給使用者呢?

在這裡結合前人寫的文章和自己的一些改動。

一般我們寫的過程中是這麼處理的:先去快取(WeakHashMap<String, Bitmap>來儲存圖片)中提取,這樣速度快又節約流量,快取中沒有再選擇本地有圖片沒有,如果兩者都沒有則只有從網路去獲取。

我們先分開講解

方法一:網路下載

方法二:從本地檔案載入

方法三:從快取中讀取

先來說方法一:

載入很多圖片的時候,我們經常用多執行緒在後臺下載,在handler中將圖片呈現在介面。

直接上程式碼

  1. //網路獲取圖片
  2.    protected Bitmap getNetBitmapByURL(String urlString) {  
  3.         URL url = null;  
  4.         InputStream inputStream = null;  
  5.         HttpURLConnection urlConnection = null;  
  6.         Bitmap bmp = null;  
  7.         try {  
  8.             url = new URL(urlString);  
  9.             urlConnection = (HttpURLConnection) url.openConnection();  
  10.             urlConnection.setRequestMethod("GET");  
  11.             urlConnection.setConnectTimeout(10000);  
  12.             inputStream = urlConnection.getInputStream();  
  13.             byte[] bt = getBytesFromStream(inputStream);  
  14.             bmp = BitmapFactory.decodeByteArray(bt, 0, bt.length);  
  15.         } catch
     (MalformedURLException e) {  
  16.             e.printStackTrace();  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         } finally {  
  20.             if (null != inputStream) {  
  21.                 try {  
  22.                     inputStream.close();  
  23.                     inputStream = null;  
  24.                 } catch (IOException e) {  
  25.                     e.printStackTrace();  
  26.                 }  
  27.             }  
  28.             if (null != urlConnection) {  
  29.                 urlConnection.disconnect();  
  30.                 urlConnection = null;  
  31.             }  
  32.         }  
  33.         return bmp;  
  34.     }  
  35.     // 資料流
  36.     privatebyte[] getBytesFromStream(InputStream inputStream) {  
  37.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  38.         byte[] b = newbyte[1024];  
  39.         int len = 0;  
  40.         while (len != -1) {  
  41.             try {  
  42.                 len = inputStream.read(b);  
  43.             } catch (IOException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46.             if (len != -1) {  
  47.                 baos.write(b, 0, len);   
  48.             }  
  49.         }  
  50.         if (inputStream != null) {  
  51.             try {  
  52.                 inputStream.close();  
  53.             } catch (IOException e) {  
  54.                 e.printStackTrace();  
  55.             }  
  56.         }  
  57.         return baos.toByteArray();  
  58.     }  
這個地方獲取了Bitmap  用handler呈現就很好搞定

再來說方法二本地獲取:

直接根據圖片路徑獲取Bitmap

  1. privateboolean cacheBmpToMemory(File file) {  
  2.         boolean sucessed = true;  
  3.         InputStream inputStream = null;  
  4.         try {  
  5.             inputStream = new FileInputStream(file);  
  6.         } catch (FileNotFoundException e) {  
  7.             e.printStackTrace();  
  8.             sucessed = false;  
  9.         }  
  10.         byte[] bs = getBytesFromStream(inputStream);  
  11.         Bitmap bitmap = BitmapFactory.decodeByteArray(bs, 0, bs.length);  
  12.         if (bitmap == null) {  
  13.             returnnull;  
  14.         }  
  15.         return bitmap;  
  16.     }  
  17. privatebyte[] getBytesFromStream(InputStream inputStream) {  
  18.         boolean b2 = true;  
  19.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  20.         byte[] b = newbyte[1024];  
  21.         int len = 0;  
  22.         while (len != -1 && b2) {  
  23.             try {  
  24.                 len = inputStream.read(b);  
  25.                 if (len != -1) {  
  26.                     baos.write(b, 0, len);  
  27.                 }  
  28.             } catch (IOException e) {  
  29.                 b2 = false;  
  30.                 try {  
  31.                     inputStream.close();  
  32.                 } catch (IOException e1) {  
  33.                 }  
  34.             }  
  35.         }  
  36.         if (inputStream != null) {  
  37.             try {  
  38.                 inputStream.close();  
  39.             } catch (IOException e) {  
  40.                 e.printStackTrace();  
  41.             }  
  42.         }  
  43.         return baos.toByteArray();  
  44.     }  




方法三快取獲取:
用弱引用:WeakHashMap<String, Bitmap>,以弱鍵 實現的基於雜湊表的 Map,在 WeakHashMap 中,當某個鍵不再正常使用時,將自動移除其條目。更精確地說,對於一個給定的鍵,其對映的存在並不阻止垃圾回收器對該鍵的丟棄,這就使該鍵成為可終止的,被終止,然後被回收。丟棄某個鍵時,其條目從對映中有效地移除。
WeakHashMap 中的每個鍵物件間接地儲存為一個弱引用的指示物件。因此,不管是在對映內還是在對映之外,只有在垃圾回收器清除某個鍵的弱引用之後,該鍵才會自動移除
這個地方結合方法一和方法二,寫了一個有關圖片快取的解決方法。

Java檔案:

ImageCache.java

  1. package com.imageService;  <