下面的代码似乎不起作用,即使该文件看起来很好。ImageIO.read返回NULL,没有错误
images = new BufferedImage[32];
FileInputStream fis = null;
for (int i = 0; i < 32; i++) {
File file = new File("tiles\\"+i+".bmp");
if (!file.exists()){
System.out.println("File "+i+" failed");
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println(e + "" + i);
}
try {
images[i] = ImageIO.read(fis);
} catch (IOException e) {
System.err.println(e + "" + i);
}
if (images[i] == null) {
System.out.println("Image "+i+" failed");
}
}
在此先感谢您的帮助。
编辑:结果是我试图Graphics.drawImage(图像[0]);;它给了我一个空指针异常。这里的代码完成很好。
编辑:更改按照建议移动了if(!file.exists()),并将该文件包装在输入流中。
[RTFM](http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#read%28java.io.File%29) – mre 2012-03-30 13:34:47
你能描述一下结果吗?一个疯狂的猜测:在不正确的路径... – 2012-03-30 13:36:59
为什么你不把你的测试,如果(!file.exists())System.out.println(“文件”+ i +“失败”);在读之前是否有 ? – 2012-03-30 13:38:37