tags:

views:

83

answers:

2

What is the best way to determine file type in android? I want to check if the given file is image or music file or video or smth else. Could you please give me a small code snippet?

A: 

Common way is by their extension only, or you can parse the header, but it should be too much slower and more complex for your apps.

Cytown
+1  A: 
  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

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(); }

  1. then use FileDataSource.getContentType() to obtain file types
dtmilano
ok. but I used android.webkit.MimeTypeMap
fixxer