9

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?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Ben
  • 91
  • 1
  • 1
  • 3
  • i'm having the same problem (android is not involved),see this link http://stackoverflow.com/questions/6308142/adding-mimetypes-to-mimetypesfiletypemap . I think we need to add a reosurce file with a complete list of mime types, not tried yet however –  Jan 26 '12 at 09:50

2 Answers2

13

Had this problem as well. According to the Java Docs the MimetypesFileTypeMap searches

  1. Programmatically added entries to the MimetypesFileTypeMap instance.
  2. The file .mime.types in the user's home directory.
  3. The file /lib/mime.types
  4. The file or resources named META-INF/mime-types
  5. The file or resources named META-INF/mime-types.default (usually only found in the activation.jar file)

If your Mimetypes are all coming out as "application/octet-stream" this implies that you have none of the above files present (or they're present but incomplete) and have not added any entries to your MimetypesFileTypeMap instance.

To solve...

MimetypesFileTypeMap mimetypes specification format

# comments start with hash marks
# format is <mime type> <space separated file extensions>
# for example, for an image mime type and a text mime type
image png tif jpg jpeg bmp
text  txt text rtf TXT
# either place those lines within the files specified by 2-4 or

MimetypesFileTypeMap mtftp = new MimetypesFileTypeMap();
mtftp.addMimeTypes("image png tif jpg jpeg bmp")

# and it's as easy as that!
Trey
  • 11,032
  • 1
  • 23
  • 21
HipsterCarlGoldstein
  • 1,029
  • 1
  • 9
  • 8
  • According to the MimetypesFileTypeMap documentation, the exact file format is this: ** text/plain txt text TXT ** (Sorry, I can't format this. :( ) – MGM Feb 23 '15 at 09:23
0

I had the same issue, you can add the right contentType to return in Headers, you can do this as follow :

public MediaType getContentType(String fileName) {
    MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
    fileTypeMap.addMimeTypes("application/msword doc");
    fileTypeMap.addMimeTypes("application/vnd.openxmlformats-officedocument.wordprocessingml.document docx");
    fileTypeMap.addMimeTypes("image/jpeg jpg jpeg");
    fileTypeMap.addMimeTypes("application/vnd.oasis.opendocument.text odt");
    fileTypeMap.addMimeTypes("application/pdf pdf");
    fileTypeMap.addMimeTypes("image/png png");
    MediaType mediaType = MediaType.parseMediaType( fileTypeMap.getContentType(fileName));
    return mediaType;
}

// Additional MIME types
fileTypeMap.addMimeTypes("application/atom+xml atom");
fileTypeMap.addMimeTypes("application/msword doc dot");
fileTypeMap.addMimeTypes("application/mspowerpoint ppt pot");
fileTypeMap.addMimeTypes("application/msexcel xls");
fileTypeMap.addMimeTypes("application/pdf pdf");
fileTypeMap.addMimeTypes("application/rdf+xml rdf rss");
fileTypeMap.addMimeTypes("application/x-vnd.openxmlformat docx docm dotx dotm");
fileTypeMap.addMimeTypes("application/x-vnd.openxmlformat xlsx xlsm");
fileTypeMap.addMimeTypes("application/x-vnd.openxmlformat pptx pptm potx");
fileTypeMap.addMimeTypes("application/x-javascript js");
fileTypeMap.addMimeTypes("application/x-rar-compressed rar");
fileTypeMap.addMimeTypes("application/x-textedit bat cmd");
fileTypeMap.addMimeTypes("application/zip zip");
fileTypeMap.addMimeTypes("audio/mpeg mp3");
fileTypeMap.addMimeTypes("image/bmp bmp");
fileTypeMap.addMimeTypes("image/gif gif");
fileTypeMap.addMimeTypes("image/jpeg jpg jpeg jpe");
fileTypeMap.addMimeTypes("image/png png");
fileTypeMap.addMimeTypes("text/css css");
fileTypeMap.addMimeTypes("text/csv csv");
fileTypeMap.addMimeTypes("text/html htm html");
fileTypeMap.addMimeTypes("text/xml xml");
fileTypeMap.addMimeTypes("video/quicktime qt mov moov");
fileTypeMap.addMimeTypes("video/mpeg mpeg mpg mpe mpv vbs mpegv");
fileTypeMap.addMimeTypes("video/msvideo avi");
fileTypeMap.addMimeTypes("video/mp4 mp4");
fileTypeMap.addMimeTypes("video/ogg ogg");
Saad Joudi
  • 595
  • 4
  • 5