In my java package, I have a file called 'prog.ico'. I'm trying to load this file, via the following code:

java.net.URL url = this.getClass().getResource("prog.ico");
java.awt.Image image = ImageIO.read( url );
System.out.println("image: " + image);

This gives the output:

image: null

What am I doing wrong? The .ico file exists in the same package as the class from which I'm running this code.

有帮助吗?

解决方案

It seems that the .ico image format is not supported. See this question and it's answer to get around this.

To prevent link rot: This solution recommends using Image4J to process .ico files.

其他提示

I've written a plugin for ImageIO that adds support for .ICO (MS Windows Icon) and .CUR (MS Windows Cursor) formats.

You can get it from GitHub here: https://github.com/haraldk/TwelveMonkeys/tree/master/imageio/imageio-ico

After you have it installed the plugin, you should be able to read your icon using the code in your original post.

I thing you must go over FileInputStream to wrap the file

 File file = new File("prog.ico"); 
 FileInputStream fis = new FileInputStream(file);  
 BufferedImage image = ImageIO.read(fis); //reading the image file  
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top