0

I am trying to save image in a user selected format from FileChooser SaveDialog. Here's my code:

java docs says the same thing for both get and set methods I dont get it.

    File f1 = new File("C:\\Users\\KIRAN\\Desktop\\Andromeda1.png");

    FileChooser stegoImageSaver = new FileChooser();
    stegoImageSaver.setTitle("Save Image File");
    stegoImageSaver.setInitialDirectory(new File("C:\\"));
    stegoImageSaver.getExtensionFilters().addAll(
        new FileChooser.ExtensionFilter("PNG Files", "*.png"),
        new FileChooser.ExtensionFilter("BMP Files", "*.bmp"),
        new FileChooser.ExtensionFilter("GIF Files", "*.gif"));
    File f1 = stegoImageSaver.showSaveDialog(null);

    ImageIO.write(img, "png", f1);

What i need is to get the extension from user from the "filechooser save dialog" and write the image with that extension.

I don't know how to use get&set extension methods in Filechooser in javaFx and couldn't find the practical implementation.

See my last line of code: I want to know how to change the "png" to whatever extension selected by the user from the "fileChooser save dialog".

Thanks in advance.

eldo
  • 1,327
  • 2
  • 16
  • 27
Can'tCode
  • 43
  • 10
  • 2
    You can check with `getSelectedExtensionFilter` which extension the user choosed. Alternativly you can check the name of the file. – IQV Feb 08 '17 at 14:28
  • This example should give you an idea of the steps needed to complete this. I think you needed a method to convert the file to the right format depending on which ext is chosen. http://java-buddy.blogspot.com/2012/05/save-file-with-javafx-filechooser.html – SedJ601 Feb 08 '17 at 14:29
  • @IQV can u give me a small example implementation of it. – Can'tCode Feb 08 '17 at 14:47
  • Sorry, I only read the docs. But I think it could be something like this: `FileChooser.ExtensionFilter fcExt = stegoImageSaver.getSelectedExtensionFilter();` and then `String ext = fcExt.getExtensions().get(0);`. The rest you have to try yourselve. – IQV Feb 08 '17 at 14:54
  • @IQV ok i'll try it, thanx – Can'tCode Feb 08 '17 at 15:04
  • 1
    it's not working. – Can'tCode Feb 08 '17 at 15:19

2 Answers2

1

The selected extension filter is only what the user selects from the combo box (or similar) in the dialog. This may or may not match the actual extension of the file. E.g. with windows you can select the png filter, but use the keyboard to input xyz.gif.

You should get the ending from the file returned from showSaveDialog:

File f1 = stegoImageSaver.showSaveDialog(null);
if (f1 != null) {
    String name = f1.getName();
    String extension = name.substring(1+name.lastIndexOf(".")).toLowerCase();
    ImageIO.write(img, extension, f1);
}
fabian
  • 80,457
  • 12
  • 86
  • 114
0

In a simple way, after you define ExtensionFilters you need, you can add them by this line

"fileChooser.getExtensionFilters().addAll(ex, exx);"

When click save the window will open and you can choice the extension that you need

    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter ex = new FileChooser.ExtensionFilter("jpg_Image", "*.jpg"); //Add as you need
    FileChooser.ExtensionFilter exx = new FileChooser.ExtensionFilter("GIF", "*.gif"); //Add as you need
    fileChooser.getExtensionFilters().addAll(ex, exx);
  File file = fileChooser.showSaveDialog(null);
    if (file != null) {
        ImageIO.write (SwingFXUtils.fromFXImage( myImgView.getImage(),null), "png", file);
    }

you can put any default extension in the last line but you still can choose from the extensions that you defined.