简体   繁体   English

从File更改为BufferedImage时出现IOException

[英]IOException while changing from File to BufferedImage

Error: Unhandled exception type IOException. 错误:未处理的异常类型IOException。

File imgLoc = new File("player.png");
BufferedImage img = ImageIO.read(imgLoc);

How do I get a bufferedImage from a file location? 如何从文件位置获取bufferedImage?

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. 等等。

Does the file exist ? 该文件是否存在? Are you by chance reading from an unexpected directory ? 您是否偶然从一个意外的目录中读取?

Try File.exists() and/or File.canRead() 尝试File.exists()和/或File.canRead()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM