0

This is my first post. Excuse me if I'm making an obvious mistake.

BufferedImage img = null;

File file = new File("com/game/assets/badlogic.jpg");

try {
   img = ImageIO.read(file);
} catch (IOException e) {
    e.printStackTrace();
}

This is my code and it can't read my image input file for some reason. This is the error message:

javax.imageio.IIOException: Can't read input file!
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
    at com.engine.Main.run(Main.java:41)
    at java.base/java.lang.Thread.run(Thread.java:844)
    at com.engine.Main.start(Main.java:32)
    at com.game.GameManager.main(GameManager.java:51)

I have no clue what am I doing wrong. Anyways Thank You, kind Strangers.

I got it to work!

BufferedImage img = null;

try {
   img = ImageIO.read(new FileInputStream("/Users/earmalys/Desktop/InteliJ projects/mantengine/src/com/game/assets/badlogic.jpg"));
} catch (IOException e) {
   e.printStackTrace();
}

But the file path is very exact. Is there anyway I can get around this?

mantasarm
  • 3
  • 3
  • 1
    Try giving absolute path. I think that is the problem move file to resource and use this link https://www.mkyong.com/java/java-read-a-file-from-resources-folder/ that tells you how to read from resource folder. – pinakin Nov 25 '19 at 18:41
  • I would go along with the assumption that the file reference you have is bad. If there's no file at that location, then try making one and see where it comes out. Also check what the absolute path is for your local reference, it may be different than what you expect. In general, I don't prefer absolute paths for this reason, if I need a relative path I explicitly specify the base path somewhere myself, rather than the exec path. – Rogue Nov 25 '19 at 18:51
  • Your path, `"com/game/assets/badlogic.jpg"`, indicates a _resource_. Resources should not be accessed via the typical file APIs (e.g. `File`). Use the APIs provided specifically to access resources, such as [`Class#getResource(String)`](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Class.html#getResource(java.lang.String)). Then try using [`ImageIO#read(URL)`](https://docs.oracle.com/en/java/javase/13/docs/api/java.desktop/javax/imageio/ImageIO.html#read(java.net.URL)). – Slaw Nov 25 '19 at 18:57
  • Amending my previous comment, I don't prefer _relative_ paths. I do prefer absolute ones. – Rogue Nov 25 '19 at 19:03
  • Duplicate? https://stackoverflow.com/questions/20389255 – killjoy Nov 25 '19 at 19:30

1 Answers1

0

Whenever I get a BufferedImage from a file, I find it much easier to use a FileInputStream rather than a File. Here's an example:

try {
    BufferedImage image = ImageIO.read(new FileInputStream("com/game/assets/badlogic.jpg"));
} 
catch (IOException e) { 
    e.printStackTrace();
}
Abra
  • 19,142
  • 7
  • 29
  • 41
Gio D
  • 124
  • 5
  • Thank you for answering ! I'm still getting an error (probably my fault). It says that "No such file or directory". – mantasarm Nov 25 '19 at 18:42
  • @GoDeadYourself I'm not sure what could be causing this. Possibly try messing around with the arguments for the fileinputstream, as sometimes you need to be more or less specific. – Gio D Nov 25 '19 at 18:47
  • 1
    Don't forget to close the `FileInputStream`. Easiest option is to use try-with-resources: `try (var fis = new FileInputStream(...)) { ... } catch (...) { ... }`. – Slaw Nov 25 '19 at 18:48