Gandalf
Gandalf

Reputation: 3257

How to save an Image?

I am using javafx.stage.FileChooser to give the user ability to browse system and save the image he/she wants. this is my code:

    Stage fileChooserStage = new Stage();
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Select an Image");
    fileChooser.getExtensionFilters().add( new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
    File file = fileChooser.showSaveDialog(fileChooserStage); 

Now i need to save the image that the user chooses in a package(folder which is in the same directory as my packages),How should i do this?

Upvotes: 0

Views: 3499

Answers (2)

SteveManC
SteveManC

Reputation: 132

If you are using java.awt.Image, you can use the SwingFXUtils.toFXImage() which will return a Buffered Image. This has a reverse option as well for converting to an fx image.

Upvotes: 1

CAG Gonzo
CAG Gonzo

Reputation: 589

Use a FileWriter that wraps file:

try (FileOutputStream out = new FileOutputStream(file))
{
    out.write(result);
}

Where result represents a byte array that represents your Image. If you are using a java.awt.Image, you can use methods from the ImageIO class. If you are using a javafx.scene.image.Image, then see this.

Upvotes: 0

Related Questions