Search code examples
androidfile-type

How to determine file types in Android


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.


Solution

    1. Include some mime.types files in your assets folder (for example from /etc/mime.types).
    2. Include activation.jar (from JAF) in your build path.
    3. 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();
      }
      
    4. Then use FileDataSource.getContentType() to obtain file types.