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?
views:
45answers:
3I 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.
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.
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].