Java mimetypes filetypemap always returns the application / octet stream on the Android simulator

I'm trying to determine the mime / media type stored on the Android device (actually a virtual device used with the emulator) I found that the resource get the MIME type from a file recommended javax activation. Mimetypes filetypemap, but when I run the following code, I get the application / octet stream of 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 with files with the following extensions: JPG, TXT, Doc, xslx and PDF, which all return the same thing What do I have to do to initialize the map? Can this library not find a list of supported MIME types on Android? Is there a better way to get the MIME type of files on Android?

Solution

And this question Mimetypes filetypemap searches based on Java documentation

>Programmatically add entries to mimetypesfiletypemap instances. > Files in the user's home directory mime. types. > File / lib / mime Types > file or resource named meta inf / MIME types > file or resource named meta inf / MIME types Default file or resource (usually found only in the activation. Jar file)

If all your mimetypes appear as "application / octet stream", it means that you do not have any of the above files (or they exist but are incomplete) and do not add any entries to the mimetypfiletypemap instance

Solve

Mimetypes filetypemap 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!
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>