ImageIO无法正确导入BufferedImage [英] ImageIO wont import BufferedImage correctly
问题描述
引发错误-
线程"Thread-3"中的异常java.lang.IllegalArgumentException:输入== null! 在javax.imageio.ImageIO.read(未知来源)
Exception in thread "Thread-3" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source)
代码-
try {
playerImage = ImageIO.read(Player.class
.getResourceAsStream("/toon.png"));
} catch (IOException e) {
e.printStackTrace();
}
文件路径-
H:\ workspace \ Isaac \ resources \ toon.png
H:\workspace\Isaac\resources\toon.png
我已经对此进行了一些研究,但是没有一种解决方案真正适合我.有什么想法吗?
Ive researched this a bit but none of the solutions have really worked for me. any ideas?
推荐答案
如果Isaac
是您的项目文件夹,则您的阅读方式应该可以使用.请检查目录resources
是否为源文件夹(在eclipse中,该目录应具有Java包覆盖的文件夹图标),否则请右键单击该目录并转到Build path
并选择Use as Source Folder
If Isaac
is your project folder, then the way your are reading should work. Please check whether the directory resources
is a source folder (in eclipse it should have an icon of a folder with an overlay of a java package), if not then right click on it and got to Build path
and choose Use as Source Folder
.
您的目录结构应如下所示:
Your directory structure should look something like this:
编译前
H:\workspace\Isaac
├───resources
│ toon.png
│
└───src
Player.java
编译后
H:\workspace\Isaac
├───bin
│ Player.class
│ toon.png
│
├───resources
│ toon.png
│
└───src
Player.java
getResourceAsStream
在bin
中查找图像,而不在resources
getResourceAsStream
looks for the image in bin
and not in the one in resources
这篇关于ImageIO无法正确导入BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!