歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 加載大圖片時報OOM的解決方案(源碼)

Android 加載大圖片時報OOM的解決方案(源碼)

日期:2017/3/1 10:45:25   编辑:Linux編程

在Android中:

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

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

  C能施用的內存 = 16M - Java某一瞬間占在校大學生創業點子用的最大內存。

  而Bitmap的生成是路程經過過程malloc進行內存分配的,占用的是C的內存。

Code :

  1. /**
  2. * 加載大圖片工具類:解決android加載大圖片時報OOM異常
  3. * 解決原理:先設置縮放選項,再讀取縮放的圖片數據到內存,規避了內存引起的OOM
  4. * @author: 張進
  5. * @time:2011/7/28
  6. */
  7. public class BitmapUtil {
  8. public static final int UNCONSTRAINED = -1;
  9. /*
  10. * 獲得設置信息
  11. */
  12. public static Options getOptions(String path){
  13. Options options = new Options();
  14. options.inJustDecodeBounds = true;//只描邊,不讀取數據
  15. BitmapFactory.decodeFile(path, options);
  16. return options;
  17. }
  18. /**
  19. * 獲得圖像
  20. * @param path
  21. * @param options
  22. * @return
  23. * @throws FileNotFoundException
  24. */
  25. public static Bitmap getBitmapByPath(String path, Options options , int screenWidth , int screenHeight)throws FileNotFoundException{
  26. File file = new File(path);
  27. if(!file.exists()){
  28. throw new FileNotFoundException();
  29. }
  30. FileInputStream in = null;
  31. in = new FileInputStream(file);
  32. if(options != null){
  33. Rect r = getScreenRegion(screenWidth,screenHeight);
  34. int w = r.width();
  35. int h = r.height();
  36. int maxSize = w > h ? w : h;
  37. int inSimpleSize = computeSampleSize(options, maxSize, w * h);
  38. options.inSampleSize = inSimpleSize; //設置縮放比例
  39. options.inJustDecodeBounds = false;
  40. }
  41. Bitmap b = BitmapFactory.decodeStream(in, null, options);
  42. try {
  43. in.close();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. return b;
  48. }
  49. private static Rect getScreenRegion(int width , int height) {
  50. return new Rect(0,0,width,height);
  51. }
  52. /**
  53. * 獲取需要進行縮放的比例,即options.inSampleSize
  54. * @param options
  55. * @param minSideLength
  56. * @param maxNumOfPixels
  57. * @return
  58. */
  59. public static int computeSampleSize(BitmapFactory.Options options,
  60. int minSideLength, int maxNumOfPixels) {
  61. int initialSize = computeInitialSampleSize(options, minSideLength,
  62. maxNumOfPixels);
  63. int roundedSize;
  64. if (initialSize <= 8) {
  65. roundedSize = 1;
  66. while (roundedSize < initialSize) {
  67. roundedSize <<= 1;
  68. }
  69. } else {
  70. roundedSize = (initialSize + 7) / 8 * 8;
  71. }
  72. return roundedSize;
  73. }
  74. private static int computeInitialSampleSize(BitmapFactory.Options options,
  75. int minSideLength, int maxNumOfPixels) {
  76. double w = options.outWidth;
  77. double h = options.outHeight;
  78. int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :
  79. (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
  80. int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :
  81. (int) Math.min(Math.floor(w / minSideLength),
  82. Math.floor(h / minSideLength));
  83. if (upperBound < lowerBound) {
  84. // return the larger one when there is no overlapping zone.
  85. return lowerBound;
  86. }
  87. if ((maxNumOfPixels == UNCONSTRAINED) &&
  88. (minSideLength == UNCONSTRAINED)) {
  89. return 1;
  90. } else if (minSideLength == UNCONSTRAINED) {
  91. return lowerBound;
  92. } else {
  93. return upperBound;
  94. }
  95. }
  96. }

工具類的使用:

  1. String path = "/sdcard/test2.jpg";
  2. try {
  3. Bitmap bitmap = BitmapUtil.getBitmapByPath(path, BitmapUtil.getOptions(path), screenWidth, screenHeight);
  4. } catch (FileNotFoundException e) {
  5. e.printStackTrace();
  6. }

Copyright © Linux教程網 All Rights Reserved