开发者

Is there a way to identify audio or image files in java, without use the file extension?

开发者 https://www.devze.com 2022-12-29 00:27 出处:网络
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?In short ... no.

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?


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].


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号