How to load Image file to ImageView?
Solution 1:
You can simply create an image with
Image image = new Image(selectedFile.toURI().toString());
and then place it in the ImageView
:
previewPicture.setImage(image);
Other constructors offer more control over resources required for loading the image. If you want to force the image to be a certain size, you can resize it on loading, which will save memory if the user chooses a large image but you only want to display a scaled-down version. Additionally, loading a large image may take time, so you should not load it on the UI thread. The Image
constructors taking string versions of URLs have options to automatically load the image in a background thread. The following forces the width and height to be both no more than 240 pixels (while maintaining the original aspect ratio), and loads the image in the background (thus not blocking the UI):
Image image = new Image(selectedFile.toURI().toString(),
240, // requested width
240, // requested height
true, // preserve ratio
true, // smooth rescaling
true // load in background
);
See the documentation for other available constructors.
Solution 2:
You create image and set to the ImageView
as follows
Image image = new Image(new FileInputStream(selectedFile));
previewPicture.setImage(image);