Java MimetypesFileTypeMap always returning application/octet-stream on Android emulator

后端 未结 1 1106
北荒
北荒 2021-01-02 09:45

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 MIM

相关标签:
1条回答
  • 2021-01-02 10:20

    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!
    
    0 讨论(0)
提交回复
热议问题