BufferedImage to Bytes Java

I am trying to get a bytearray from bufferedImage, but the length of my ByteArrayOutputstream is always zero and I am not getting any bytes. This is what I do

any ideas why this doesn't work

BufferedImage scaledPicture = ..... ByteArrayOutputStream baos = new ByteArrayOutputStream( ); ImageIO.write(scaledPicture, extension, baos); baos.flush(); byte[] toByteArray = baos.toByteArray(); baos.close(); return toByteArray; 

Any ideas?

+7
source share
2 answers

What is the return value of ImageIO.write ? Does ImageIO.getImageWritersByFormatName with at least 1 ImageWriter ? If not, the format may not be supported.

+3
source

Does the call return ImageIO.write (...) 'true? If not, the JVM may not find the author of the appropriate format.

+1
source

All Articles