can't create a image file from BASE64

By | July 12, 2018

Well, i’m trying to send a picture from android to java, if i make it with compression it works really good, but i need to make it without compression beacuse i need a good or normal quality.
FixBitmap is my current Bitmap picture

FixBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byteArray = byteArrayOutputStream.toByteArray();
ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT);

Length 43388

String file=request.getParameter("image_data");
String filename=rt.getId()+"_"+rt.getName()+".png";
BufferedImage image = null;
byte[] imageByte;
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(file);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image =;
File outputfile = new File(filename);
ImageIO.write(image, "png", outputfile);
Path destinationFile = Paths.get(getServletContext().getRealPath("/")+"uploads\\", filename);
}catch(Exception ex){
System.out.println("Error :"+ex.getMessage());

This code actually works as i said, but the compression make it looks in a very bad quality, so i tried to make it without compression, just converting my bitmap to a byte array, just like this

ByteBuffer buffer = ByteBuffer.allocate(FixBitmap.getRowBytes() * 
byteArray = buffer.array();
ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT);

Length 252107

The code on my java side it’s the same but now it doesn’t work, it just shows me this error :

java.lang.IllegalArgumentException: image == null!

So I decided to print the length because maybe there is some restrictions about this…
so I hope you can help me with this (just send/get the picture without compression)


Leave a Reply

Your email address will not be published. Required fields are marked *