IOException while changing from File to BufferedImage
-
21-09-2019 - |
Question
Error: Unhandled exception type IOException.
File imgLoc = new File("player.png");
BufferedImage img = ImageIO.read(imgLoc);
How do I get a bufferedImage from a file location?
Solution
The cause of your problem is best determined by examining a stacktrace for the exception.
As a temporary measure, replace those two lines with the following:
File imgLoc = new File("player.png");
BufferedImage img;
try {
img = ImageIO.read(imgLoc);
} catch (IOException ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
throw ex;
}
to send some diagnostics to standard error. Run the modified app and post the resulting output.
Possible causes include:
- The file name is wrong,
- The file is not in the app's current directory,
- The file is not readable by the app due to operating system access controls,
- The file is readable but there is something wrong with its format,
- etcetera.
OTHER TIPS
Does the file exist ? Are you by chance reading from an unexpected directory ?
Try File.exists() and/or File.canRead()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow