20

The following code seems not to work, even though the file appears to be found just fine.

    images = new BufferedImage[32];
    FileInputStream fis = null;
    for (int i = 0; i < 32; i++) {
        File file = new File("tiles\\"+i+".bmp");
        if (!file.exists()){
            System.out.println("File  "+i+" failed");
        }
        try { 
            fis = new FileInputStream(file); 
        } catch (FileNotFoundException e) { 
            System.err.println(e + "" + i); 
        }
        try { 
            images[i] = ImageIO.read(fis); 
        } catch (IOException e) { 
            System.err.println(e + "" + i); 
        }
        if (images[i] == null) {
            System.out.println("Image "+i+" failed");
        }
    }

Thanks in advance for any help.

Edit: The result is me attempting to Graphics.drawImage(images[0]);, and it giving me a null pointer exception. This code here completes fine.

Edit: Changed moved the if(!file.exists()) as suggested, and wrapped the file in an input stream.

d.raev
  • 9,216
  • 8
  • 58
  • 79
Naberius
  • 235
  • 1
  • 2
  • 7

5 Answers5

49

ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.

Any other image type will return null without error.

reference: http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

I do realize this is not a solution to the specific original problem but it is a solution to the question asked.

Irrationalkilla
  • 592
  • 4
  • 7
9

ImageIO.read(file); will return null if no registered ImageReader is found. Please check whether you have registered any ImageReader.

I think this code snippet could help you

File file = new File("bear.jpg"); // I have bear.jpg in my working directory  
    FileInputStream fis = new FileInputStream(file);  
    BufferedImage image = ImageIO.read(fis); //reading the image file  

You just need to wrap the file into an FileInputStream and then pass it to read()

Fay Boisam
  • 127
  • 4
  • 10
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed. – Naberius Mar 30 '12 at 13:59
  • Can you say what are your image file names in the disk? – Chandra Sekhar Mar 30 '12 at 14:07
  • @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case. – Chandra Sekhar Mar 30 '12 at 14:16
  • Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder. – Naberius Apr 02 '12 at 13:12
  • @Naberius Then try using File file = new File("tiles"+i+".bmp"); this. – Chandra Sekhar Apr 02 '12 at 13:20
  • Seems to be working now, for some reason. I guess it didn't like my naming, or the folder. Also, the initial answer will be pretty helpful in the future. – Naberius Apr 03 '12 at 12:50
  • This doesn't work in Java 8, the `BufferedImage` is null if I use it on an e.g. ".tif" image file that returns `true` for `file.exists()` (the `FileInputStream` isn't null). – Neph Aug 22 '23 at 14:30
0

Add the following dependencies:

<dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-jpeg2000</artifactId>
        <version>1.4.0</version>
    </dependency>
AM13
  • 661
  • 1
  • 8
  • 18
-1

Try wrap you InputStream into BufferedInputStream:

fis = new FileInputStream(file); ==> new BufferedInputStream(new FileInputStream(file));

Ahmed MANSOUR
  • 2,369
  • 2
  • 27
  • 35
-1

Use new File(getClass().getResource("tiles\\"+i+".bmp"));

MichaelJohn
  • 195
  • 1
  • 2
  • 12