0

Okay so I have spent about 15 hours trying to figure this out. I am working on this program that gets exported to a non-runnable jar file. The issue is I am trying to load an image in the jar and set it to a variable.

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. The image is a .png which I have heard is compatible with ImageIO.read(). I am using an API so that is what the log() lines are.

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 = 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.

EDIT: I also just tried making mainPaint a BufferedImage and using:

    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?

Opoz
  • 1
  • 2
  • ImageIO.read catches a lesser exception IllegalArgumentException - if input is null. is also possible so dont get too excited – gpasch May 29 '16 at 19:33
  • I'm printing out url.toString() and it is printing the location of the image, so url is not null. – Opoz May 29 '16 at 19:41
  • @gpasch `ImageIO.read` *throws* an `IllegalArgumentException` if input is `null`. It doesn't catch that exception. – Harald K May 30 '16 at 07:37
  • @Opoz Can you attach your PNG (preferably extracted from the JAR, in case it has been mangled in any way)? Without the file in question ([*complete and verifiable*, remember?](http://stackoverflow.com/help/mcve)) we can only guess and speculate... – Harald K May 30 '16 at 07:40

2 Answers2

1

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

Here is another SO post that explain where you can do that.

Hexfire
  • 5,945
  • 8
  • 32
  • 42
aibrahim
  • 229
  • 2
  • 4
0

ImageIO.read() will load only GIF, PNG, JPEG, BMP, and WBMP image types. Any other image type will return null without error.

It could answer to your question .

Avi Youkhananov
  • 717
  • 5
  • 9
  • This is not correct. The ist of supported formats is here https://imageio.readthedocs.io/en/stable/formats.html – Dave Durbin Sep 13 '18 at 10:08