简体   繁体   English

Java ImageIO.read(URL)不应返回null

[英]Java ImageIO.read(URL) returning null when it shouldn't

Okay so I have spent about 15 hours trying to figure this out. 好的,所以我花了大约15个小时来解决这个问题。 I am working on this program that gets exported to a non-runnable jar file. 我正在将此程序导出到不可运行的jar文件中。 The issue is I am trying to load an image in the jar and set it to a variable. 问题是我试图将图像加载到jar中并将其设置为变量。

I HAVE looked at other posts and I think I have tried everything I could find but nothing seems to work. 我看过其他帖子,我想我已经尽力找到了所有可以找到的东西,但似乎没有任何效果。

I am not asking how to FIND the image as I can get the URL of the image, but then ImageIO.read(URL) is not throwing any exception, but returning null. 我不是在问如何查找图像,因为我可以获得图像的URL,但是ImageIO.read(URL)不会引发任何异常,而是返回null。 The image is a .png which I have heard is compatible with ImageIO.read(). 该图像是我听说过的.png与ImageIO.read()兼容。 I am using an API so that is what the log() lines are. 我正在使用一个API,所以log()行是什么。

Any help is appreciated. 任何帮助表示赞赏。 Thank you! 谢谢!

My Project: 我的项目:

    Project
    ->src
    -->package
    --->Main.java
    --->paint.png

My Code: 我的代码:

In my main method: (mainPaint is a private Image) 在我的主要方法中:(mainPaint是私有图像)

    mainPaint = getImage("paint.png");

The method: 方法:

private Image getImage(String fileName) {

    URL url = getClass().getResource(fileName);
    BufferedImage image = null;

    log(url.toString()); // To make sure I have the correct file
    // returning jar:file:/C:/Users/Me/MyJar.jar!/package/paint.png

    try {
        image = ImageIO.read(url);
    } catch (IOException e1) {
        log("Error converting url to image.");
        // This is not happening
    }

    if (image == null) {
        log("Image is null.");
        // This is happening
    }

    return image;
}

Is the URL invalid? 网址无效吗? Am I just missing something? 我只是想念什么吗? I'm just trying to save the local image in the jar as an Image object, I feel like this is way too difficult for what I am trying to do. 我只是想将本地图像保存在jar中作为Image对象,我觉得这对我想做的事情来说太困难了。

EDIT: I also just tried making mainPaint a BufferedImage and using: 编辑:我也只是试图使mainPaint一个BufferedImage并使用:

    Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(fileName));
    if(image == null) {
        log("Image is null");
    }
    log("Height: " + image.getHeight(null));
    log("Width: " + image.getWidth(null));

    BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(image, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;

The height and width of the image are returning -1? 图片的高度和宽度返回-1?

ImageIO.read() will load only GIF, PNG, JPEG, BMP, and WBMP image types. ImageIO.read()将仅加载GIF,PNG,JPEG,BMP和WBMP图像类型。 Any other image type will return null without error. 任何其他图像类型都将返回null且没有错误。

It could answer to your question . 它可以回答您的问题。

ImageIO.read() will not load SVG files, so if your images are in SVG formate you will need to add plugins to it to support SVG ImageIO.read()将不会加载SVG文件,因此,如果您的图像位于SVG formate中,则需要向其添加插件以支持SVG

Here is another SO post that explain where you can do that. 是另一篇SO帖子,解释了在哪里可以做到这一点。

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

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