从java中的项目文件夹加载图像 [英] load an image from project's folder in java

查看:179
本文介绍了从java中的项目文件夹加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载我的projet文件夹中的图片: /src/images/URL.jpg

I want to load an image which is in my projet folder as : /src/images/URL.jpg

我试过这段代码:

BufferedImage image = ImageIO.read(getClass().getResource("/images/URL.jpg"));

但我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at Personel.PersonnelMainForm.print(PersonnelMainForm.java:464)

如何解决这个问题?

推荐答案

你可以试试 的版本读取,它将File作为参数。

You can try this version of read, which takes File as an argument.

BufferedImage image = ImageIO.read(new File("path"));

其中路径是您提交的文件的路径,绝对的或相对的,你需要的。

where path is the path to you file, absolute or relative as you need.

如果你真的想把它作为一种资源加载,另一种选择就是编辑你的类路径,按照这个问题

Another option, if you really want to load it as a resource, would be editing your classpath, as per this question.

这篇关于从java中的项目文件夹加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆