I'm trying to read a byte array of an image using the following code but it throws an out of memory exception. I have commented where the exception occurs.
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();
}
You are ignoring that how much number of bytes are actually read.
int numberofBytesRead=dis.read(bBuffer, 0, bBuffer.length);
baos.write(bBuffer, 0, numberOfBytesRead);