1

I'm writing a simple game using a data driven design in which I read in the images for the various elements of the game. As such I've written a parser to read-in the files, confirm that they're an acceptable image format, and create a new Sprite (class I've written) using the files.

I read in the files using ImageIO.read() as seen in the example below.

toAdd = new Sprite(ImageIO.read(targetFile));

The stack trace for the exception is as follows:

javax.imageio.IIOException: Can't create an ImageInputStream!
    at javax.imageio.ImageIO.read(ImageIO.java:1280)
    at edu.moravian.utilities.dataParsers.GraphicsDataParser.readInSprites(GraphicsDataParser.java:53)

I've got no idea what's causing this. The application isn't multi-threaded, the images are in the correct directory and are confirmed to exist, and I have the permissions necessary to access the images.

What might I be missing?

Myles Barros
  • 11
  • 1
  • 4
  • Have you tried opening up the file with a regular input stream? And does it exist? – Jens Egholm Mar 05 '13 at 18:24
  • I've added a check to the code that confirms that the files exist. How would you recommend going about opening the images with a regular input stream?The main appeal of simply using ImageIO.read() was that I wouldn't have to make use of input streams directly. – Myles Barros Mar 05 '13 at 18:29
  • Indeed - and that would be my choice. Bit the ImageIO is very high-level (and so is your error message, really), so looking at a more simpler InputStream might give you a clue at what's going wrong. In a case like this I would probably just try and open the file and traverse the content to see if anything goes wrong (disc I/O, permissions etc.). Another thing to do is to use a static resourse that you *know* will work and taking it from there. Sorry I can't give any more pointers - I've never experienced this kind of faults. – Jens Egholm Mar 05 '13 at 22:38
  • But if nothing helps, perhaps you can throw in some more code to explain how and where it crashes? What does the targetFile look like for instance? – Jens Egholm Mar 05 '13 at 22:46
  • The targetFile in my test case is a simple PNG that I used and successfully read-in in a separate project in a similar manner. I've tried a few other images, such as a couple of JPGs and a different PNG, and the error is recurring. Although I don't know how it might be relevant, I'm temporarily using an absolute file path for the image directory that I'm reading from as I was unable to get the directory recognized in any of the usual local folders. I'll try looking into using an InputStream more directly in the meantime. Thanks a lot for your help, I really appreciate it. – Myles Barros Mar 06 '13 at 02:23
  • 1
    Well I feel silly. Rather than passing in the desired File directly I attempted a lazy solution: I wrapped it as a FileInputStream and passed that in in its place and it ran just fine - green tests across the board. Thanks for the suggestion. It's a bit of a bummer not to know what the root cause was but I'm glad that the code is at least running. – Myles Barros Mar 06 '13 at 02:42
  • Glad to help :-) And nice you got it sorted. Guess that's the dark side of a high-level API for ya... – Jens Egholm Mar 06 '13 at 14:48

1 Answers1

0

One possible cause could be that you are reading a jpeg which is using a CMYK color model. ImageIO does not support these by default.

There are possibilites to add that function though. For example look at this answer: https://stackoverflow.com/a/16149142/6276704

Tigerware
  • 3,196
  • 2
  • 23
  • 39