Dana
Dana

Reputation: 41

ImageIO.createImageInputStream is returning null, how come?

This used to work and now it isn't working. I didn't change the portion where the Image is suppose to stream so I don't know why this isn't working. Here's a snippet of my code:

File f = new File(filepath);
applicantImage=new ImageDetail(filepath);

Iterator<ImageReader> readers =
     ImageIO.getImageReadersBySuffix(applicantImage.getFile_extension());
ImageReader reader = readers.next();

ImageInputStream iis = ImageIO.createImageInputStream(f);

filepath is the absolute filepath to the image and the image is confirmed to be at that location on the server. Everything works all fine and dandy until it gets to ImageIO.createImageInputStream(f). It is returning null now. I read javadoc that it takes in a File. So why is it returning a null now? Anyone know? None of the codes posted above were changed in my editing so I don't understand why it worked before the change and why it doesn't work now. Any help is much appreciated! Thanks!

Upvotes: 4

Views: 7979

Answers (2)

Nizar B.
Nizar B.

Reputation: 3118

You need to set the input before:

reader.setInput(iis);
dimension = new Dimension(reader.getWidth(0), reader.getHeight(0));
System.out.print(dimension);

Upvotes: 0

trashgod
trashgod

Reputation: 205875

Try using an InputStream constructed with the File.

ImageIO.createImageInputStream(new FileInputStream(f));

Here's a related example.

Addendum: Looking closer at ImageIO.createImageInputStream(), it does say that a File is permitted. Indeed, your original code works on my platform (Mac OS X), leading me to speculate that the null result may be peculiar to your platform's ImageInputStreamSpi implementation. For a non-existent File f,

ImageIO.createImageInputStream(f);

I get a reasonable exception:

Exception in thread "main" java.lang.IllegalStateException: Input not set!
    at com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:784)
    at javax.imageio.ImageReader.read(ImageReader.java:923)
    at cli.GifBounds.search(GifBounds.java:22)
    at cli.GifBounds.main(GifBounds.java:14)

Upvotes: 5

Related Questions