1

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.

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    what's the result of `file.exists()`? – turbo Dec 20 '13 at 21:06
  • @turbo it gives false. But the file definitely exists in the same package, under the same name (prog.ico) – Ali Dec 20 '13 at 21:08
  • @turbo I checked it by making a file from the same path (by getting `url.getPath()` ) and that also gives false – Ali Dec 20 '13 at 21:09
  • have you tried an absolute path? – turbo Dec 20 '13 at 21:10
  • What's printed when you execute `System.out.println(this.getClass().getName())`? Are you sure the file exists in the same package as the one printed? How are you compiling your classes? – JB Nizet Dec 20 '13 at 21:11
  • @turbo Trying the absolute path (by making a new `File` and passing it the absolute path, then passing this file into `ImageIO.read` also returns null, without throwing any exception. Perhaps ImageIO can't load .ico files? – Ali Dec 20 '13 at 21:15
  • @JBNizet Yes, the file definitely exists. I'm compiling in netbeans. – Ali Dec 20 '13 at 21:15
  • Check if this.getClass().getResourceAsStream() returns null. If it does, then the resource is not there. If it does not, then it's an ImageIO problem. – JB Nizet Dec 20 '13 at 21:17
  • @JBNizet If I replace the file's name in code to a `.jpg` one in the same folder, then it loads up file. So the problem must be that `.ico` file can't be read.. – Ali Dec 20 '13 at 21:18
  • 1
    I think you're right, this might help: http://stackoverflow.com/questions/11090508/how-to-get-favicon-ico-from-a-website-using-java – turbo Dec 20 '13 at 21:20
  • @turbo If you want to post that as answer, I can accept. – Ali Dec 20 '13 at 21:21

3 Answers3

2

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.

Community
  • 1
  • 1
turbo
  • 1,887
  • 2
  • 21
  • 35
1

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.

Harald K
  • 26,314
  • 7
  • 65
  • 111
0

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  
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • As I mentioned in comments, just changing the file to a .jpg fixes the problem, so I think the issue is that .ico files are not supported. – Ali Dec 20 '13 at 21:36