Android快取和讀取快取,網路載入圖片
阿新 • • 發佈:2019-01-24
在開發中,遇到很多大量圖片呈現給使用者,那麼我們怎麼又快又高效的顯示給使用者呢?
在這裡結合前人寫的文章和自己的一些改動。
一般我們寫的過程中是這麼處理的:先去快取(WeakHashMap<String, Bitmap>來儲存圖片)中提取,這樣速度快又節約流量,快取中沒有再選擇本地有圖片沒有,如果兩者都沒有則只有從網路去獲取。
我們先分開講解
方法一:網路下載
方法二:從本地檔案載入
方法三:從快取中讀取
先來說方法一:
載入很多圖片的時候,我們經常用多執行緒在後臺下載,在handler中將圖片呈現在介面。
直接上程式碼
- //網路獲取圖片
- protected Bitmap getNetBitmapByURL(String urlString) {
- URL url = null;
- InputStream inputStream = null;
- HttpURLConnection urlConnection = null;
- Bitmap bmp = null;
- try {
- url = new URL(urlString);
-
urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.setRequestMethod("GET");
- urlConnection.setConnectTimeout(10000);
- inputStream = urlConnection.getInputStream();
- byte[] bt = getBytesFromStream(inputStream);
- bmp = BitmapFactory.decodeByteArray(bt, 0, bt.length);
-
} catch
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (null != inputStream) {
- try {
- inputStream.close();
- inputStream = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != urlConnection) {
- urlConnection.disconnect();
- urlConnection = null;
- }
- }
- return bmp;
- }
- // 資料流
- privatebyte[] getBytesFromStream(InputStream inputStream) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] b = newbyte[1024];
- int len = 0;
- while (len != -1) {
- try {
- len = inputStream.read(b);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (len != -1) {
- baos.write(b, 0, len);
- }
- }
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return baos.toByteArray();
- }
再來說方法二本地獲取:
直接根據圖片路徑獲取Bitmap
- privateboolean cacheBmpToMemory(File file) {
- boolean sucessed = true;
- InputStream inputStream = null;
- try {
- inputStream = new FileInputStream(file);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- sucessed = false;
- }
- byte[] bs = getBytesFromStream(inputStream);
- Bitmap bitmap = BitmapFactory.decodeByteArray(bs, 0, bs.length);
- if (bitmap == null) {
- returnnull;
- }
- return bitmap;
- }
- privatebyte[] getBytesFromStream(InputStream inputStream) {
- boolean b2 = true;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] b = newbyte[1024];
- int len = 0;
- while (len != -1 && b2) {
- try {
- len = inputStream.read(b);
- if (len != -1) {
- baos.write(b, 0, len);
- }
- } catch (IOException e) {
- b2 = false;
- try {
- inputStream.close();
- } catch (IOException e1) {
- }
- }
- }
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return baos.toByteArray();
- }
方法三快取獲取:
用弱引用:WeakHashMap<String, Bitmap>,以弱鍵 實現的基於雜湊表的 Map,在 WeakHashMap 中,當某個鍵不再正常使用時,將自動移除其條目。更精確地說,對於一個給定的鍵,其對映的存在並不阻止垃圾回收器對該鍵的丟棄,這就使該鍵成為可終止的,被終止,然後被回收。丟棄某個鍵時,其條目從對映中有效地移除。
WeakHashMap 中的每個鍵物件間接地儲存為一個弱引用的指示物件。因此,不管是在對映內還是在對映之外,只有在垃圾回收器清除某個鍵的弱引用之後,該鍵才會自動移除
這個地方結合方法一和方法二,寫了一個有關圖片快取的解決方法。
Java檔案:
ImageCache.java
- package com.imageService; <