How can I load images from a computer directory in JAVAFX

I am trying to upload images from my computer folder to a thumbnail wall. I read in a thread from another forum that the ImageView instance variable "url" does not support system paths. I tried with the solution there, but it throws an exception: java.lang.OutOfMemoryError: Java heap space as it keeps reading the file .

Another problem is that it continues to warn me about using the javafx.ext -> SwingUtils.toFXImage method.

I also tried to enter the url like this:

 "file://localhost//Users/USER/Pictures/Camera/test/1.JPG" 

I tried to display multiple images, but it always displays only 3-4 images.

I checked with the error function set from ImageView , it does not indicate that an error occurred while reading my images.

Are there any alternatives?

The code

 function load() { println("RUNTIME {Runtime.getRuntime().maxMemory()}"); System.gc(); Runtime.getRuntime().freeMemory(); //MAC Folder PATH var path: String = "/Users/username/Pictures/camera/test/1.JPG";; var file: File = new File(path); //http://download.oracle.com/docs/cd/E17802_01/javafx/javafx/1.3/docs/api/javafx.ext.swing/javafx.ext.swing.SwingUtils.html //public toFXImage(image: java.awt.image.BufferedImage) : Image //Creates a JavaFX Image from a BufferedImage. img = SwingUtils.toFXImage(ImageIO.read(file)); } 
+6
source share
5 answers

It is not clear what exactly you are trying to do. If you are talking about JavaFX 2.0, the following code works. If you upload a lot of images and you need to save memory, you need to create enough ImageView for the number that you want to display at a time. Then, when you are viewing images, you can change the Image object contained in the ImageView .

 public void start(Stage primaryStage) { primaryStage.setTitle("Hello World"); StackPane root = new StackPane(); Scene scene = new Scene(root, 300, 250); File file = new File("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png"); Image image = new Image(file.toURI().toString()); ImageView iv = new ImageView(image); root.getChildren().add(iv); primaryStage.setScene(scene); primaryStage.show(); } 
+26
source

simple: open the image using a browser and copy the entire URL of the picture and paste it as a parameter of the Image object. DO NOT REMOVE the "///: file" because it makes the image load. you get your other logic from there. happy coding for example

 Image image = new Image("file:///C:/Users/Nigel/Desktop/my_image.png"); ImageView imgview = new ImageView(image); 
+5
source

Sorry if my answer was a little late, but it was my approach, and it works 100% wherever you put your file 1. Enter the image in file 2. View the file in URI 3. Set uri.toString () to the image

ex code:

 File imageFile = new File("path/to/image/outside/your/jar/awesomeless.jpg"); String fileLocation = imageFile.toURI().toString(); Image fxImage = new Image(fileLocation); 

or you can just put it all together like this:

 Image fxImage = new Image(new File("path/.../awesomemore.jpg").toURI().toString()); 

and please, if anyone knows a better approach let us know!

+2
source

Another solution is to pass an InputStream to the constructor of the Image class; and working...

 public void start(Stage primaryStage) { primaryStage.setTitle("Hello World"); StackPane root = new StackPane(); Scene scene = new Scene(root, 300, 250); java.io.FileInputStream fis = new FileInputStream("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png"); ImageView iv = new ImageView(new Image(fis)); root.getChildren().add(iv); primaryStage.setScene(scene); primaryStage.show(); } 
+1
source

None of the previous answers worked for me. However, this one, which I saw some time ago, but cannot find the source link, works flawlessly.

 @Override public void start(Stage primaryStage) throws Exception { //create a pane to hold the image views Pane pane = new HBox(10); pane.setPadding(new Insets(5,5,5,5)); //create the image to be used! Image image = new Image("/Content/vortex.jpg"); //set some custom properties and add an image ImageView imageView = new ImageView(image); imageView.setFitHeight(100); imageView.setFitWidth(100); pane.getChildren().add(imageView); //add the second image view with this image and no custom properties pane.getChildren().add(new ImageView(image)); ImageView imageView2 = new ImageView(image); imageView2.setRotate(45); pane.getChildren().add(imageView2); Scene scene = new Scene(pane, 200, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { Application.launch(args); } 

The key is that when creating the image, START the path with "/" (that is: "/Content/vortex.jpg"). Note that in this setting, the root folder is the src folder in most IDEs.

+1
source

Source: https://habr.com/ru/post/899667/


All Articles