2013-03-06 116 views
0

我收到了內存不足的錯誤:內存不足錯誤 - BitmapFactory.decodeStream

下面是我的代碼:

public static Bitmap decodeSampledBitmapFromResource(InputStream inputStream,int reqWidth, int reqHeight) { 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      byte[] buffer = new byte[1024]; 
      int len; 
      try { 
      while ((len = inputStream.read(buffer)) > -1) { 
       baos.write(buffer, 0, len); 
      } 
      baos.flush(); 
      InputStream is1 = new ByteArrayInputStream(baos.toByteArray()); 
      InputStream is2 = new ByteArrayInputStream(baos.toByteArray()); 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inDither=false;  
     options.inPurgeable=true;     
     options.inInputShareable=true; 
     options.inJustDecodeBounds = true; 
     // BitmapFactory.decodeResource(res, resId, options); 
     BitmapFactory.decodeStream(is1,null,options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 

     return BitmapFactory.decodeStream(is2,null,options); // error at this line 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } 
    } 

我在這行

BitmapFactory.decodeStream(is2,null,options); 

我得到錯誤得到這個錯誤Out of memory on a 3250016-byte allocation.

我看過很多文章,但仍無法找到soluti在這種情況下。

+0

你得到了什麼錯誤?請粘貼LogCat或StackTrace。 – 2013-03-06 12:41:24

+0

爲什麼您首先將流加載到內存?從哪裏獲取圖像從服務器或從本地文件? – Triode 2013-03-06 12:41:30

+0

我有文件夾調用數據我從那使用類加載器,它返回我一個輸入流imges,我傳遞給此方法。 – Goofy 2013-03-06 12:43:41

回答

2

請不要讓這個函數靜態存在靜態成員的內存問題。

秒您沒有使用BitmapFactory.decodeStream(is1,null,options);任何地方都可以從代碼中刪除它。

也試試這個代碼,我用它來從SD卡路徑位圖,你可以很容易地修改,以滿足您的需求,我用它來處理類似的情況,它永遠不會失敗。

public Bitmap getImage(String path) throws IOException 
    { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options);   
     int srcWidth = options.outWidth; 
     int srcHeight = options.outHeight; 
     int[] newWH = new int[2]; 
     newWH[0] = srcWidth/2; 
     newWH[1] = (newWH[0]*srcHeight)/srcWidth; 

     int inSampleSize = 1; 
     while(srcWidth/2 >= newWH[0]){ 
      srcWidth /= 2; 
      srcHeight /= 2; 
      inSampleSize *= 2; 
     } 

     //  float desiredScale = (float) newWH[0]/srcWidth; 
     // Decode with inSampleSize 
     options.inJustDecodeBounds = false; 
     options.inDither = false; 
     options.inSampleSize = inSampleSize; 
     options.inScaled = false; 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options); 
     ExifInterface exif = new ExifInterface(path); 
     String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
     System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s); 
     Matrix matrix = new Matrix(); 
     float rotation = rotationForImage(Add_View_Images_Activity.this, Uri.fromFile(new File(path))); 
     if (rotation != 0f) { 
      matrix.preRotate(rotation); 
     } 
     int newh = (w * sampledSrcBitmap.getHeight()) /sampledSrcBitmap.getWidth(); 
     Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true); 
     Bitmap resizedBitmap = Bitmap.createBitmap(
       r, 0, 0, w, newh, matrix, true); 

     return resizedBitmap; 
    } 
+0

嘿感謝,但它這裏給出的http:// developer.android.com/training/displaying-bitmaps/load-bitmap.html使用方法,還我沒有從路SD卡,我有一個輸入流,所以我必須通過 – Goofy 2013-03-06 13:39:23

+0

什麼爲w?這裏 – Goofy 2013-03-06 13:50:56

+0

你在嗎? – Goofy 2013-03-06 13:55:56