-1

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

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();
}
4

1 回答 1

1

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

int numberofBytesRead=dis.read(bBuffer, 0, bBuffer.length);
baos.write(bBuffer, 0, numberOfBytesRead);
于 2013-03-05T11:57:40.257 回答