How to determine file types in Android

What is the best way to determine file type in Android?

I want to check if a given file is an image, a music file, etc.

+4
source share
3 answers
  • Include some mime.types files in your resources folder (e.g. 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 get the file types.

+4
source
 public static String getMimeType(String fileUrl) { String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl); final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); return (mimeType != null) ? mimeType : "unknown"; } 
+2
source

The usual way is to just expand them, or you can parse the header, but it should be too slow and more complicated for your applications.

0
source

All Articles