Android讀取本地圖片,圖片太大導致OOM問題。
阿新 • • 發佈:2019-02-18
這是畢設中,我要讀取本地相機拍攝到的相片時,遇到的問題,提示OOM,然後強制關閉,這篇部落格點選開啟連結裡提到了一種方法能解決這個問題。我把主要內容寫下:
使用時直接呼叫其中的getBitmap()即可。public final class BitMapUtil { private static final Size ZERO_SIZE = new Size(0, 0); private static final Options OPTIONS_GET_SIZE = new Options(); private static final Options OPTIONS_DECODE = new Options(); private static final byte[] LOCKED = new byte[0]; // 此物件用來保持Bitmap的回收順序,保證最後使用的圖片被回收 private static final LinkedList CACHE_ENTRIES = new LinkedList(); // 執行緒請求建立圖片的佇列 private static final Queue TASK_QUEUE = new LinkedList(); // 儲存佇列中正在處理的圖片的key,有效防止重複新增到請求建立佇列 private static final Set TASK_QUEUE_INDEX = new HashSet(); // 快取Bitmap private static final Map IMG_CACHE_INDEX = new HashMap(); // 通過圖片路徑,圖片大小 private static int CACHE_SIZE = 20; // 快取圖片數量 static { OPTIONS_GET_SIZE.inJustDecodeBounds = true; // 初始化建立圖片執行緒,並等待處理 new Thread() { { setDaemon(true); } public void run() { while (true) { synchronized (TASK_QUEUE) { if (TASK_QUEUE.isEmpty()) { try { TASK_QUEUE.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } QueueEntry entry = TASK_QUEUE.poll(); String key = createKey(entry.path, entry.width, entry.height); TASK_QUEUE_INDEX.remove(key); createBitmap(entry.path, entry.width, entry.height); } } }.start(); } public static Bitmap getBitmap(String path, int width, int height) { if(path==null){ return null; } Bitmap bitMap = null; try { if (CACHE_ENTRIES.size() >= CACHE_SIZE) { destoryLast(); } bitMap = useBitmap(path, width, height); if (bitMap != null && !bitMap.isRecycled()) { return bitMap; } bitMap = createBitmap(path, width, height); String key = createKey(path, width, height); synchronized (LOCKED) { IMG_CACHE_INDEX.put(key, bitMap); CACHE_ENTRIES.addFirst(key); } } catch (OutOfMemoryError err) { destoryLast(); System.out.println(CACHE_SIZE); return createBitmap(path, width, height); } return bitMap; } public static Size getBitMapSize(String path) { File file = new File(path); if (file.exists()) { InputStream in = null; try { in = new FileInputStream(file); BitmapFactory.decodeStream(in, null, OPTIONS_GET_SIZE); return new Size(OPTIONS_GET_SIZE.outWidth, OPTIONS_GET_SIZE.outHeight); } catch (FileNotFoundException e) { return ZERO_SIZE; } finally { closeInputStream(in); } } return ZERO_SIZE; } // ------------------------------------------------------------------ private Methods // 將圖片加入佇列頭 private static Bitmap useBitmap(String path, int width, int height) { Bitmap bitMap = null; String key = createKey(path, width, height); synchronized (LOCKED) { bitMap = IMG_CACHE_INDEX.get(key); if (null != bitMap) { if (CACHE_ENTRIES.remove(key)) { CACHE_ENTRIES.addFirst(key); } } } return bitMap; } // 回收最後一張圖片 private static void destoryLast() { synchronized (LOCKED) { String key = CACHE_ENTRIES.removeLast(); if (key.length() > 0) { Bitmap bitMap = IMG_CACHE_INDEX.remove(key); if (bitMap != null && !bitMap.isRecycled()) { bitMap.recycle(); bitMap = null; } } } } // 建立鍵 private static String createKey(String path, int width, int height) { if (null == path || path.length() == 0) { return ""; } return path + "_" + width + "_" + height; } // 通過圖片路徑,寬度高度建立一個Bitmap物件 private static Bitmap createBitmap(String path, int width, int height) { File file = new File(path); if (file.exists()) { InputStream in = null; try { in = new FileInputStream(file); Size size = getBitMapSize(path); if (size.equals(ZERO_SIZE)) { return null; } int scale = 1; int a = size.getWidth() / width; int b = size.getHeight() / height; scale = Math.max(a, b); synchronized (OPTIONS_DECODE) { OPTIONS_DECODE.inSampleSize = scale; Bitmap bitMap = BitmapFactory.decodeStream(in, null, OPTIONS_DECODE); return bitMap; } } catch (FileNotFoundException e) { Log.v("BitMapUtil","createBitmap=="+e.toString()); } finally { closeInputStream(in); } } return null; } // 關閉輸入流 private static void closeInputStream(InputStream in) { if (null != in) { try { in.close(); } catch (IOException e) { Log.v("BitMapUtil","closeInputStream=="+e.toString()); } } } // 圖片大小 static class Size { private int width, height; Size(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public int getHeight() { return height; } } // 佇列快取引數物件 static class QueueEntry { public String path; public int width; public int height; } }