Want to save BufferedImage as jpg but my code saves it as textfile

By | July 12, 2018
Questions:

I want to export a BufferedImage as jpg, but in this code it will get saved as a textfile. How can i fix this?

    public void saveImage(BufferedImage im) {
    JFileChooser fc = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg", ".jpg");
    fc.setAcceptAllFileFilterUsed(false);
    fc.setFileFilter(filter);
    int ret = fc.showSaveDialog(null);
    File f = fc.getSelectedFile();

    if (ret == JFileChooser.APPROVE_OPTION) {

        try {

            ImageIO.write(im, "jpg", f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
Answers:

Leave a Reply

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