android載入大圖片時報OOM的解決方案

來源:互聯網
上載者:User

 

在Android中:

 

  1.一個進程的記憶體可以由2個部門組成:java 施用記憶體 ,C 施用記憶體 ,這兩個記憶體的和必需小於16M,不然就會出現各人熟悉的OOM,這個就是熬頭種OOM的情況。

 

  2.一朝記憶體配置給Java後,以後這塊記憶體縱然開釋後,也只能給Java的施用,這個估計跟java虛擬機器裡把記憶體分成好幾塊進行緩衝的原因有關,反正C就別想用到這塊的記憶體了,所以要是Java突然佔用了一個大塊記憶體,縱然很快開釋了:

 

  C能施用的記憶體= 16M - Java某一瞬間佔在校大學生創業點子用的最大記憶體。

 

  而Bitmap的產生是路程經過過程malloc進行記憶體配置的,佔用的是C的記憶體。

 

 

 

Code :

 

 

 

/**

 * 載入大圖片工具類:解決android載入大圖片時報OOM異常

 * 解決原理:先設定縮放選項,再讀取縮放的圖片資料到記憶體,規避了記憶體引起的OOM

 * @author: 張進

 

 * @time:2011/7/28

 */

public class BitmapUtil {

 

    public static final int UNCONSTRAINED = -1;

   

    /*

  * 獲得設定資訊

  */

 public static Options getOptions(String path){

  Options options = new Options();

  options.inJustDecodeBounds = true;//只描邊,不讀取資料

  BitmapFactory.decodeFile(path, options);

  return options;

 }

 

 

 /**

  * 獲得映像

  * @param path

  * @param options

  * @return

  * @throws FileNotFoundException

  */

 public static Bitmap getBitmapByPath(String path, Options options , int screenWidth , int screenHeight)throws FileNotFoundException{

  File file = new File(path);

  if(!file.exists()){

   throw new FileNotFoundException();

  }

  FileInputStream in = null;

  in = new FileInputStream(file);

  if(options != null){

   Rect r = getScreenRegion(screenWidth,screenHeight);

   int w = r.width();

   int h = r.height();

   int maxSize = w > h ? w : h;

   int inSimpleSize = computeSampleSize(options, maxSize, w * h);

   options.inSampleSize = inSimpleSize; //設定縮放比例

   options.inJustDecodeBounds = false;

  }

  Bitmap b = BitmapFactory.decodeStream(in, null, options);

  try {

   in.close();

  } catch (IOException e) {

   e.printStackTrace();

  }

  return b;

 }

 

 

   

 private static Rect getScreenRegion(int width , int height) {

  return new Rect(0,0,width,height);

 }

 

 

 /**

  * 擷取需要進行縮放的比例,即options.inSampleSize

  * @param options

  * @param minSideLength

  * @param maxNumOfPixels

  * @return

  */

 public static int computeSampleSize(BitmapFactory.Options options,

            int minSideLength, int maxNumOfPixels) {

        int initialSize = computeInitialSampleSize(options, minSideLength,

                maxNumOfPixels);

 

        int roundedSize;

        if (initialSize <= 8) {

            roundedSize = 1;

            while (roundedSize < initialSize) {

                roundedSize <<= 1;

            }

        } else {

            roundedSize = (initialSize + 7) / 8 * 8;

        }

 

        return roundedSize;

    }

 

    private static int computeInitialSampleSize(BitmapFactory.Options options,

            int minSideLength, int maxNumOfPixels) {

        double w = options.outWidth;

        double h = options.outHeight;

 

        int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :

                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));

        int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :

                (int) Math.min(Math.floor(w / minSideLength),

                Math.floor(h / minSideLength));

 

        if (upperBound < lowerBound) {

            // return the larger one when there is no overlapping zone.

            return lowerBound;

        }

 

        if ((maxNumOfPixels == UNCONSTRAINED) &&

                (minSideLength == UNCONSTRAINED)) {

            return 1;

        } else if (minSideLength == UNCONSTRAINED) {

            return lowerBound;

        } else {

            return upperBound;

        }

    }

   

 

}

 

 

 

 

 

 

工具類的使用:

 

 String path = "/sdcard/test2.jpg";

     try {

   Bitmap bitmap = BitmapUtil.getBitmapByPath(path, BitmapUtil.getOptions(path), screenWidth, screenHeight);

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.