抛出OutOfMemoryError的ByteArrayOutputStream

时间:2013-03-05 11:46:04

标签: java android out-of-memory bytearrayoutputstream

我正在尝试使用以下代码读取图像的字节数组,但它会引发内存不足异常。我已经评论了异常发生的地方。

byte[] bBuffer = new byte[300000]; // 
ByteArrayBuffer baf = new ByteArrayBuffer(300000);
int total = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
    int read = 0;           
    while ((dis.read(bBuffer, 0, bBuffer.length)) != -1) {
        baos.write(bBuffer, 0, bBuffer.length); // Exception occurs here        
        Log.d("outputImageBytes", "Reading Image Bytes: " + bBuffer.length + " " + read++ );
    }
    //baos.flush();
}
catch (Exception e)
{   
    Log.e("outputImageBytes", "Exception Occured while reading image bytes: " + e.getMessage());
    e.getMessage();
}

1 个答案:

答案 0 :(得分:1)

你忽略了实际读取了多少字节。

int numberofBytesRead=dis.read(bBuffer, 0, bBuffer.length);
baos.write(bBuffer, 0, numberOfBytesRead);