0

I would like to load an image from my current src directory where the java class files are located as well. However, I always get an IOException..

And how can I make sure the file gets loaded properly on Mac/Linux as well on Windows?

My code so far:

String dir = System.getProperty("user.dir") + "/Logo_transparent.png";
File imageFile = new File(dir);
BufferedImage bufferedImage = null;

try {
    bufferedImage = ImageIO.read(imageFile);
} catch (IOException e) {
    System.out.println(e.getMessage());
    System.out.println(dir);
    System.out.println();
}

IOException message:

Can't read input file!

(My path is correct - is it because of the space between Google and Drive?)

/Users/myMac/Google Drive/Privat/Programming/Logo_transparent.png

Kind regards and thank you!

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
Jan
  • 1,180
  • 3
  • 23
  • 60
  • Are you sure that is the correct path? – SamHoque Dec 12 '18 at 10:58
  • Yes, I am! - Is there a way to test it? – Jan Dec 12 '18 at 10:58
  • Can you do `System.out.println(dir);` and tell us what it returns – SamHoque Dec 12 '18 at 10:59
  • Return of that is: "/Users/myMac/Google Drive/Privat/Programming" – Jan Dec 12 '18 at 10:59
  • okay can you do `System.out.println(imageFile.exists());` and see what that returns? – SamHoque Dec 12 '18 at 10:59
  • 1
    @Jan, the question seems to be incorrect: you're saying that you want to get this image from src directory, but referring us to `/Users/myMac/Google Drive/Privat/Programming/Logo_transparent.png`. Clarify what exactly you want to do? – Sergey Prokofiev Dec 12 '18 at 12:12
  • `ImageIO.read` will throw this specific exception only in one case, that is when the input file is not readable (as in `!file.isReadable()`) by the Java process. Usually, this means you have to change the file access rights of your file (and/or parent folders). Could also be that the Java process is running under the wrong user. – Harald K Dec 12 '18 at 12:22

3 Answers3

0

I think It's because you didn't create the file, You can create the file if it doesn't exist by using this code

if(!imageFile.exists()) imageFile.createNewFile();

You're code will look like this

String dir = System.getProperty("user.dir") + "/Logo_transparent.png";
File imageFile = new File(dir);
BufferedImage bufferedImage = null;
try {
    if(!imageFile.exists()) imageFile.createNewFile();
    bufferedImage = ImageIO.read(imageFile);
} catch (IOException e) {
    System.out.println(e.getMessage());
    System.out.println(dir);
    System.out.println();
}

Also you shouldn't concat child files like that instead pass it as a second argument.

File imageFile = new File(System.getProperty("user.dir"), "Logo_transparent.png");
SamHoque
  • 2,978
  • 2
  • 13
  • 43
0

If your image files will be packaged together with your class files (for example in the same .jar) you should not use File but read it as a resource:

bufferedImage = ImageIO.read(this.getClass().getResourceAsStream("/Logo_transparent.png"));

Notice the '/' before the file name. This means to search in the root path of the classpath. If you specify without / it will search in the package of this (the current class)

this.getClass().getResourceAsStream("Logo_transparent.png")
Conffusion
  • 4,335
  • 2
  • 16
  • 28
-1

You can try to build the absolute path to the image like here and read it afterward.

awgold90
  • 72
  • 13