What is the best way to determine a file type in Android?
I want to check if the given file is an image, a music file, etc.
mime.types
files in your assets folder (for example from /etc/mime.types).activation.jar
(from JAF) in your build path.Use something like this:
try {
MimetypesFileTypeMap mftm =
new MimetypesFileTypeMap(getAssets().open("mime.types"));
FileTypeMap.setDefaultFileTypeMap(mftm);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Then use FileDataSource.getContentType()
to obtain file types.