tags:

views:

45

answers:

3

Supposing new audio and image types will be created, I want my filter to separate the images and audio files. Is there a way to recognize then?

+1  A: 

I can suggest you only exception-based solution i.e. try to read each file with ImageIO and if exception is thrown then it's not an image file. But it's a bad solution.

You'd better find an opportunity to distinguish them in some other more logical way.

Roman
+1 I liked. If new types of image appear, the ImageIO will be updated, so my app. I'll wait more opinions.
Tom Brito
But ImageIO doesn't recognize *all* types of images that exist even now... call `ImageIO.getReaderFormatNames()` / `getReaderMIMETypes()` to see which are supported. You can add plugins to support new image types, but you'd have to know the format beforehand.
Nate
maybe some apache lib does?
Tom Brito
+2  A: 

You could look into the files to detect the actual media type. Look at Java Mime Magic Library. It is similar to the file command under unix.

deamon
is there a generic method to say if it is an audio or image, independent of the extension?
Tom Brito
the purpose of this lib is to not rely upon the file extension.
deamon
+2  A: 

In short ... no.

In long, assuming we have no knowledge of media files and types [ie no knowledge of file extensions, data format, etc], the only means to identify a kind of file is to probe its data and identify it - which requires intimate knowledge of the media format, something we do not have.

However, if our operating system contains these media files, chances are someone within the system does know how to open or manipulate them. Therefore it is logical to assume this application would register the media type [and other metadata such as file extension, default player, etc] with the system.

So if you are keen on remaining dynamic, you should probably find a means of communicating with the underlying system for media events [such as when a new media format is encountered and registered].

johnny g