設置恰當的inSampleSize能夠使BitmapFactory分配更少的空間以消除該錯誤。inSampleSize的具體含義請參考SDK文檔。例如:java
BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = 4; Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
設置恰當的inSampleSize是解決該問題的關鍵之一。BitmapFactory.Options提供了另外一個成員inJustDecodeBounds。算法
BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
設置inJustDecodeBounds爲true後,decodeFile並不分配空間,但可計算出原始圖片的長度和寬度,即opts.width和opts.height。有了這兩個參數,再經過必定的算法,便可獲得一個恰當的inSampleSize。數組
查看Android源碼,Android提供了一種動態計算的方法。網絡
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 == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 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 == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }
使用該算法,就可動態計算出圖片的inSampleSize。app
BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(imageFile, opts); opts.inSampleSize = computeSampleSize(opts, -1, 128*128); opts.inJustDecodeBounds = false; try { Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts); imageView.setImageBitmap(bmp); } catch (OutOfMemoryError err) { }
另外,能夠經過Bitmap.recycle()方法來釋放位圖所佔的空間,固然前提是位圖沒有被使用。ide
獲取縮略圖關鍵代碼:url
byte[] imageByte=getImageFromURL(urlPath[i].trim());
//如下是把圖片轉化爲縮略圖再加載 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //首先設置.inJustDecodeBounds爲true Bitmap bitmap=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length, options); //這時獲取到的bitmap是null的,還沒有調用系統內存資源 options.inJustDecodeBounds = false; 獲得圖片有寬和高的options對象後,設置.inJustDecodeBounds爲false。 int be = (int)(options.outHeight / (float)200); if (be <= 0) be = 1; options.inSampleSize = be; //計算獲得圖片縮小倍數 bitmaps[i]=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); //獲取真正的圖片對象(縮略圖) /** * 根據圖片網絡地址獲取圖片的byte[]類型數據 * @param urlPath 圖片網絡地址 * @return 圖片數據 */ public byte[] getImageFromURL(String urlPath){ byte[] data=null; InputStream is=null; HttpURLConnection conn=null; try { URL url=new URL(urlPath); conn=(HttpURLConnection) url.openConnection(); conn.setDoInput(true); //conn.setDoOutput(true); conn.setRequestMethod("GET" ); conn.setConnectTimeout(6000 ); is=conn.getInputStream(); if(conn.getResponseCode()==200 ){ data=readInputStream(is); } else System.out.println("發生異常!" ); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ conn.disconnect(); try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return data; }
/** * 讀取InputStream數據,轉爲byte[]數據類型 * @param is InputStream數據 * @return 返回byte[]數據 */ public byte[] readInputStream(InputStream is) { ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte[] buffer=new byte[1024 ]; int length=-1 ; try { while((length=is.read(buffer))!=-1 ){ baos.write(buffer, 0 , length); } baos.flush(); } catch (IOException e) { e.printStackTrace(); } byte[] data=baos.toByteArray(); try { is.close(); baos.close(); } catch (IOException e) { e.printStackTrace(); } return data; } /** * 根據網絡圖片地址集批量獲取網絡圖片 * @param urlPath 網絡圖片地址數組 * @return 返回Bitmap數據類型的數組 */ public Bitmap[] getBitmapArray(String[] urlPath){ int length=urlPath.length; if(urlPath==null||length<1 ){ return null; } else{ Bitmap[] bitmaps=new Bitmap[length]; for (int i = 0 ; i < length; i++) { byte[] imageByte=getImageFromURL(urlPath[i].trim()); //如下是把圖片轉化爲縮略圖再加載 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap=BitmapFactory.decodeByteArray(imageByte, 0 , imageByte.length, options); options.inJustDecodeBounds = false; int be = (int)(options.outHeight / (float)200 ); if (be <= 0 ) be = 1 ; options.inSampleSize = be; bitmaps[i]=BitmapFactory.decodeByteArray(imageByte, 0 , imageByte.length,options); } return bitmaps; } }