0

Is there a way to check a File is readable as a BufferedImage without doing the try with ImageIO.read(File) ?

I could eventually extract the mime type of the file, but is there a list of defaultly supported mime types for BufferedImage or something like that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • Do be host, by the time you extract the mime type, look up the available service providers and determine if any can actually read the mime type, it would have be faster a simpler to just do ImageIO.read and catch the error – MadProgrammer Dec 03 '12 at 21:03

1 Answers1

2

I don't know about defaultly, but ImageIO.getReaderMIMETypes() will give you the list of currently supported readers.

The correct thing to do is probably to try ImageIO.createImageInputStream(file) - if it doesn't throw an IOException, but returns null, the image is not readable.

Note that ImageIO.read(file) will also not throw an exception for an unsupported MIME format, it will return null. Exceptions will only be thrown for a supported file that can't be decoded (ie the file is corrupted), or IO errors (obviously).

Dmitri
  • 8,999
  • 5
  • 36
  • 43
  • Thanks! I read the javadoc too fast and didn't notice it was only throwing when there was an error, and returning null when not an image :) – Sebastien Lorber Dec 04 '12 at 09:08