8

I know this has been asked many times and I have searched far and wide for a solution to this probably simple problem. I'm trying to follow the simple javaFX component tutorials on Oracle's website. I can define an image this way:

Image img = new Image("images/portal.png", 50, 50, true, true);

This works when the image is in a folder inside the "src" folder, but I'm trying to get it to find the image when I have the image folder outside of the "src" folder, like this:

project_root/
 |---src/
      |---Main.java
 |---images/
      |---portal.png    

How can I make this work? All I get is errors saying "Invalid URL or resource not found". I've tried to use the absolute path, tried putting ".." infront of it, tried "HS-Graph/images/portal.png" and everything inbetween :( Thank you!

Zorobay
  • 557
  • 1
  • 6
  • 23
  • Construct a URL. Save it with url.toString(). Then use new Image(String). – edharned Dec 12 '14 at 15:16
  • I'm not sure what you mean, why would that make any difference? It will still result in a string with the same text as in my case. – Zorobay Dec 12 '14 at 16:18
  • Use a File file = new File("portal.png"); if (file.exists()) String tdir = afile.getAbsolutePath(); or you can use file.toURI() or whatever you need. Is that helpful? – edharned Dec 12 '14 at 19:47
  • Well it doesn't work :(. file.exists() is false. It can't find my file. There must surely be a simple way to set relative paths that are outside your src folder! I mean, it worked in slick! Thanks for your help though... – Zorobay Dec 13 '14 at 01:11
  • That you cannot find the file is your task. Read the JavaDoc on java.io.File. You're only looking in the current directory. You need to walk through the file system to find the root directory, or whatever is pertinent to your environment. You may be able to search here or google for file-walking, etc. to get some idea of how to do it. If the file is in your file system then you can find it. – edharned Dec 13 '14 at 15:43

3 Answers3

21

I'm going to answer my own question as I actually found a solution to this! My solution is to use the "file:" prefix when specifying a path. So:

Image img = new Image("file:images/portal.png");

Works perfectly when the image file is outside of my src folder!

Zorobay
  • 557
  • 1
  • 6
  • 23
  • this example is not working. I'm add prefix file: do not work. – Ng Sharma Jul 19 '18 at 08:08
  • Are you using Windows? I just tried it again and it works just fine. Did you place your image folder outside of the project folder? – Zorobay Jul 23 '18 at 07:17
  • Okay I Will try Thank for Reply – Ng Sharma Jul 23 '18 at 07:24
  • Weird. I did this and now I get a crazy error in the console that just constantly flickers. I actually can't even copy it because it is flickering. – Riley Fitzpatrick Feb 04 '19 at 02:19
  • Work too if you use it on a .fxml instead of `url="@../../../../resources/iconfinder_heartbeat_1930261.png"` `url="file:resources/iconfinder_heartbeat_1930261.png"` – Juan Feb 27 '19 at 13:08
4

I think you are running into issues because the Images folder is outside of the scope of your project. You could consider changing the structure of your project.

Ex:

->src
|-->main
    |--->java
          |-->(default package)
    |--->resources
          |-->images

Then you should be able to access your image with the path ./src/main/resources/images/portal.png

EricF
  • 418
  • 3
  • 6
  • Well it's not outside of my project, it's just under the Project folder (HS-Graph). I know I have programmed a game using Slick, where I put the image folder as I have here, and it worked :/ – Zorobay Dec 12 '14 at 16:15
  • 1
    But the folder is NOT part of the classpath hence it can not be found at runtime – tomsontom Dec 12 '14 at 17:37
  • So you mean there is no way of doing it the same way as I want it to be? I HAVE to put my images, audio, video and what not inside my src folder? – Zorobay Dec 13 '14 at 01:12
2

Let me try to put it in this way; If you want to use the image found in the different directory it is better to specify its relative path. Example if i was looking for the portal image on my ImageFile. I'll do as follows:

Image img = new Image("file:images/portal.png");
eli
  • 8,571
  • 4
  • 30
  • 40
  • 2
    Shouldn't use absolute paths. What if he wanted to distribute the program? The user may not store it on the `C` drive, and will most definitely not have the same machine username. It's better to access the user folder via `System.getProperty("user.home")`, but even then, the program would have to store all it's resources there (which isn't uncommon, but the restriction isn't always preferred) – Vince Jul 19 '17 at 14:32
  • i have tried this suggestion ' Image img = new Image("file:images/portal.png"); ' but it didn't work for me thast why i used that – eli Jul 19 '17 at 14:37
  • 1
    His image was in his project folder, while your example shows the image being on the desktop (not in the project folder). Put your image in the project (under a folder named `images`) and try again. His question is about the image not being in `ProjectName/src`, but he still has it in `ProjectName/images`. – Vince Jul 19 '17 at 14:46
  • It needs to be in `ProjectName/images`. He `file:` part tells the program the path refers to a file within the project. He then follows this with `images/...` which tells us it's in the images folder. The question is about loading images that *aren't* in the `src` folder (source folder). If you had 25 rep, I could invite you to a chat to further explain. Edit some posts (+2 rep each time it's approved), fix up this answer so it justifies an up vote (don't use absolute paths). – Vince Jul 19 '17 at 15:41