Loading image from file inside project folder

I am trying to load an image from a file without using FileChooser . Folders:

 TestProject -src --application ---(all_the_classes_i'm_using.java) -assets --drawIcon.png 

I want to upload an image to the assets folder. I tried:

 Image image = new Image("../assets/drawIcon.png") Image image = new Image(getClass().getResourceAsStream("../assets/drawIcon.png")) 

I tried this with the path line "/TestProject/assets/drawIcon.png", but nothing. I do not understand how to download this image!

+6
source share
1 answer

Set the assets directory as a resource directory, and then load the image as a resource from the location "/drawIcon.png":

 URL url = getClass().getResource("/drawIcon.png"); Image image = ImageIO.read(url); 

If you want to create a javafx image:

 Image image = new Image("/drawIcon.png"); 

In this case, also mark this folder as a resource folder.

More information here: https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html

+10
source

All Articles