【问题标题】:Loading images in java (eclipse vs IntelliJ)在java中加载图像(eclipse vs IntelliJ)
【发布时间】:2016-10-17 21:32:48
【问题描述】:

加载图片:
项目>res>img.png(路径=“res/img.png”)

BufferedImage image = loadImage(path);

LoadImage 在哪里:

protected BufferedImage loadImage(String path) {
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File(path));
    } catch(IOException e) {
        System.err.println("could not load: " + path);
    }
    return img;
}


使用 Eclipse 的人使用过:
(路径 = "/img.png")

BufferedImage image = null;
    try {
        image = ImageIO.read(Sprite.class.getResourceAsStream(path));
    } catch (IOException e) {       
        e.printStackTrace();
    }

但在 IntelliJ 中使用它会给出:

线程“Game_main”java.lang.IllegalArgumentException 中的异常: 输入 == null!

为什么 getResourceAsStream 失败?

【问题讨论】:

    标签: java eclipse image intellij-idea


    【解决方案1】:

    getResourceAsStream()(默认情况下)使用系统类加载器来查找文件。因此,资源目录必须位于类路径中 - 检查 IntelliJ 项目是否正确包含 res 目录并将其标记为资源目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2019-01-08
      • 1970-01-01
      • 2015-08-12
      相关资源
      最近更新 更多