我收到以下错误:
javax.imageio.IIOException: Can't read input file!
Resource not found: C:\icon.gif
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at connector.SystemTrayCreator.createImage(SystemTrayCreator.java:98)
at connector.SystemTrayCreator.create(SystemTrayCreator.java:36)
at connector.Start.main(Start.java:14)
Exception in thread "main" java.lang.IllegalArgumentException: creating TrayIcon with null Image
at java.awt.TrayIcon.<init>(TrayIcon.java:168)
at connector.SystemTrayCreator.create(SystemTrayCreator.java:36)
at connector.Start.main(Start.java:14)
Java Result: 1
以下代码:
final TrayIcon trayIcon =
new TrayIcon(createImage("C:\\icon.gif", "Tray icon"));
与
protected static Image createImage(String path, String description) {
Image image = null;
try {
File f = new File(path);
image = ImageIO.read(f);
} catch (Exception e) {
e.printStackTrace();
}
if (image == null) {
Debugger.showMessage("Resource not found: " + path);
return null;
} else {
return (new ImageIcon(image, description)).getImage();
}
}
我很确定路径是正确的。此外,名称匹配。
任何人都知道什么是错的?
答案 0 :(得分:2)
您的代码似乎很好。我理论上可能出现两个问题:
文件权限问题。可能将文件放在除驱动器根目录之外的其他位置,并明确检查其权限。您可以直接从代码中调用canRead()
- 这就是ImageIO.read
所做的事情。它必须返回true
。
path
参数末尾可能有一些空格。使用trim()
消除这种可能性。