How to determine file types in Android

android, file-type

Solution

- Include some `mime.types` files in your assets folder (for example from /etc/mime.types).

- Include `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.

Problem

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.

Original source