我想使用下面的代码读取图像的字节数组,但它会引发内存不足异常。我评论了发生异常的位置。ByteArrayOutputStream抛出OutOfMemoryError
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();
}
在写入'baos'时,您忽略了'dis.read(..)'的值。它返回读取的实际字节数,现在你总是插入缓冲区的总长度。 – 2013-03-05 11:51:57
有点困惑,请问你能举个例子吗? :) – 2013-03-05 11:54:34