android 加載圖片輕鬆避免OOM(out of memory)
在使用android加載圖片的時候,經常會出現內存溢出,主要是由於android可使用的內存太小,而通過代碼加載進來的圖片,並不會被GC回收,於是我寫了一個工具類用來加載圖片,並且建立緩存,輕鬆避免內存溢出,廢話不多說,上代碼
- package l.test1.util;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.Map;
- import java.util.Queue;
- import java.util.Set;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.BitmapFactory.Options;
- /**
- * Bitmap工具類,緩存用過的指定數量的圖片,使用此工具類,不再需要手動管理Bitmap內存 原理:
- * 用一個隊列保存使用Bitmap的順序,每次使用Bitmap將對象移動到隊列頭 當內存不夠,或者達到製定的緩存數量的時候,回收隊列尾部圖片
- * 保證當前使用最多的圖片得到最長時間的緩存,提高速度
- *
- * @author liaoxingliao
- *
- */
- 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];
- private static final LinkedList<String> CACHE_ENTRIES = new LinkedList<String>(); // 此對象用來保持Bitmap的回收順序,保證最後使用的圖片被回收
- private static final Queue<QueueEntry> TASK_QUEUE = new LinkedList<QueueEntry>(); // 線程請求創建圖片的隊列
- private static final Set<String> TASK_QUEUE_INDEX = new HashSet<String>(); // 保存隊列中正在處理的圖片的key,有效防止重複添加到請求創建隊列
- private static final Map<String, Bitmap> IMG_CACHE_INDEX = new HashMap<String, Bitmap>(); // 緩存Bitmap
- // 通過圖片路徑,圖片大小
- private static int CACHE_SIZE = 200; // 緩存圖片數量
- 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);
- //修正過的代碼
- getBitmap(entry.path,entry.width,entry.height);
- }
- }
- }.start();
- }
- /**
- * 創建一張圖片 如果緩存中已經存在,則返回緩存中的圖,否則創建一個新的對象,並加入緩存
- * 寬度,高度,為了縮放原圖減少內存的,如果輸入的寬,高,比原圖大,返回原圖
- *
- * @param path 圖片物理路徑 (必須是本地路徑,不能是網絡路徑)
- * @param width 需要的寬度
- * @param height 需要的高度
- * @return
- */
- public static Bitmap getBitmap(String path, int width, int height) {
- 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 getBitmap(path, width, height);
- }
- return bitMap;
- }
- /**
- * 設置緩存圖片數量 如果輸入負數,會產生異常
- *
- * @param size
- */
- public static void setCacheSize(int size) {
- if (size <= 0) {
- throw new RuntimeException("size :" + size);
- }
- while (size < CACHE_ENTRIES.size()) {
- destoryLast();
- }
- CACHE_SIZE = size;
- }
- /**
- * 加入一個圖片處理請求到圖片創建隊列
- *
- * @param path
- * 圖片路徑(本地)
- * @param width
- * 圖片寬度
- * @param height
- * 圖片高度
- */
- public static void addTask(String path, int width, int height) {
- QueueEntry entry = new QueueEntry();
- entry.path = path;
- entry.width = width;
- entry.height = height;
- synchronized (TASK_QUEUE) {
- String key = createKey(path, width, height);
- if (!TASK_QUEUE_INDEX.contains(key)
- && !IMG_CACHE_INDEX.containsKey(key)) {
- TASK_QUEUE.add(entry);
- TASK_QUEUE_INDEX.add(key);
- TASK_QUEUE.notify();
- }
- }
- }
- /**
- * 通過圖片路徑返回圖片實際大小
- * @param path 圖片物理路徑
- * @return
- */
- 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) {
- e.printStackTrace();
- } finally {
- closeInputStream(in);
- }
- }
- return null;
- }
- // 關閉輸入流
- private static void closeInputStream(InputStream in) {
- if (null != in) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // 圖片大小
- 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;
- }
- }
最後更新:2017-04-02 17:09:26