Convert Bufferedimage to File?

Asked

Viewed 917 times

1

I need to convert a BufferedImage for a File. I tried it this way, but it didn’t work:

File file = null;
ImageIO.write(image, "jpg", file);

image is the type BufferedImage.

That was the mistake:

Exception in thread "AWT-Eventqueue-0" java.lang.Illegalargumentexception: output == null! at javax.imageio.Imageio.write(Unknown Source)*

  • Just to be clear: what you want is to save the image in a file. File is just a reference to the file, it contains no data, so it is not really possible convert data in a File, but create a reference to a file and then write the bytes or characters in the file.

1 answer

2


The problem was that you prompted your File as null.

File file  = new File("image.jpg");
ImageIO.write(image, "jpg", file );

You need to specify the image path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.