2013-12-10 41 views
0

我曾經使用下面的代碼將inputstream對象轉換爲位圖。但它返回「內存不足錯誤」,並且BitmapFactory選項總是返回零。如何在嘗試將輸入流對象轉換爲android中的位圖時防止內存不足異常?

S3ObjectInputStream inputStreamReceiptObject = objectReceiptFromAmazonS3 
          .getObjectContent(); 
Bitmap bitmapImageFromAmazon = null; 
         try { 
if (inputStreamReceiptObject != null){ 
BitmapFactory.Options o = new BitmapFactory.Options(); 
           o.inSampleSize = 8; 
           o.inJustDecodeBounds = true; 
           bitmapImageFromAmazon = BitmapFactory.decodeStream(inputStreamReceiptObject,null,o); // o is always null 
           if(bitmapImageFromAmazon == null){ 
            System.out.println("Bitmap null"); 
           } 

          } 

高級謝謝你的幫助!

SOLUTION:(感謝尊敬的唐和尊敬Akshat的地塊)

ByteArrayOutputStream baos = null ; 
           InputStream is1 = null,is2 = null; 
           try { 
             baos = new ByteArrayOutputStream(); 
             // Fake code simulating the copy 
             // You can generally do better with nio if you need... 
             // And please, unlike me, do something about the Exceptions :D 
             byte[] buffer = new byte[1024]; 
             int len; 
             while ((len = inputStreamReceiptObject.read(buffer)) > -1) { 
              baos.write(buffer, 0, len); 
             } 
             baos.flush(); 

             // Open new InputStreams using the recorded bytes 
             // Can be repeated as many times as you wish 
             is1 = new ByteArrayInputStream(baos.toByteArray()); 
             is2 = new ByteArrayInputStream(baos.toByteArray()); 

             bitmapImageFromAmazon = getBitmapFromInputStream(is1,is2); 

             if(bitmapImageFromAmazon == null) 
              System.out.println("IMAGE NULL"); 
             else 
              System.out.println("IMAGE NOT NULL"); 

           } catch (Exception e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } finally { 
            baos.close(); 
            is1.close(); 
            is2.close(); 
           } 


    public Bitmap getBitmapFromInputStream(InputStream is1,InputStream is2) throws IOException { 
     Bitmap bitmap = null; 

      try { 
       //Decode image size 
       BitmapFactory.Options o = new BitmapFactory.Options(); 
       o.inJustDecodeBounds = true; 
       BitmapFactory.decodeStream(is1,null,o); 

       //Find the correct scale value. It should be the power of 2. 
       int scale=1; 


       //Decode with inSampleSize 
       BitmapFactory.Options o2 = new BitmapFactory.Options(); 
       o2.inSampleSize=scale; 
       bitmap = BitmapFactory.decodeStream(is2, null, o2); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     return bitmap; 

    } 

回答

1

你爲什麼不嘗試和縮放位圖圖像下來?這就是爲什麼大多您的應用程序顯示OOM異常

原因
 BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inScaled = false; 
     o.inJustDecodeBounds = true; 
     FileInputStream stream1 = new FileInputStream(f); 
     BitmapFactory.decodeStream(stream1, null, o); 
     stream1.close(); 

     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 70; //This is the max size of the bitmap in kilobytes 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE 
        || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     FileInputStream stream2 = new FileInputStream(f); 
     Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2); 
     stream2.close(); 
     return bitmap; 
+0

感謝您的幫助。我的是輸入流,而不是文件輸入流。但我正在接收位圖工廠選項對象o爲null。該怎麼辦? – Sakthimuthiah

+0

@Sakthimuthiah你確定你在'BitmapFactory.Options o = new BitmapFactory.Options();'行後正確檢查你的調試器。你是否也輸入了正確的BitmapFactory? –

1

的內存不足的錯誤應該是因爲它說:你沒有你的設備上有足夠的內存來渲染整個圖像。您需要確保從S3下載的映像對於設備來說不算太大。

幫助調試,試運行下載一個較小的圖像,看看是否仍收到OOM錯誤

URLConnection conn = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Victoria_Parade_postcard.jpg/120px-Victoria_Parade_postcard.jpg").openConnection(); 
InputStream stream = conn.getInputStream(); 
Bitmap image = BitmapFactory.decodeStream(stream, null, null); 

傳遞到decodeStream鄰是因爲OOM錯誤的空(它一定去當您在調試器中檢查它時超出範圍)。

+0

感謝您的幫助。從S3下載的圖像太小,設備也有足夠的空間。但仍然是它的接收錯誤和o總是返回null – Sakthimuthiah

+0

您的意思是bitmapImageFromAmazon是否爲空?因爲您將它作爲參數傳遞,所以o不能爲null,並且您不能將參數的值設置爲從方法內部爲null。 – don