I am trying to determine the Mime/Media Type of files stored on an Android device (actually a virtual device I'm using with the emulator). I found this resource Get the MIME Type from a File which recommends the javax.activation.MimetypesFileTypeMap
, however when I run the following code I get application/octet-stream
for all file types:
MimetypesFileTypeMap map = new MimetypesFileTypeMap();
File dir = Environment.getExternalStorageDirectory();
File[] files = dir.listFiles();
String mimeType;
for( int i = 0; i < files.length; ++i ) {
if( files[i].isDirectory() == false ) {
mimeType = map.getContentType(files[i]);
if( mimeType.toLowerCase().equals("application/octet-stream") ) {
Log.d("mytag",
String.format( "Unable to determine the mime type of file %s.",
files[i].getName()));
}
}
}
I have tested this with files having the following extensions: jpg, txt, doc, xslx and pdf and they all return the same thing. Is there something I have to do to initialize the map? Can this library not find the list of supported mime-types on Android? Is there a better way to get the mime type of a file on Android?