Java的MimetypesFileTypeMap中总是在Android模拟器返回应用程序/八位字节流 [英] Java MimetypesFileTypeMap always returning application/octet-stream on Android emulator

查看:907
本文介绍了Java的MimetypesFileTypeMap中总是在Android模拟器返回应用程序/八位字节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定存储在Android设备(我用的仿真器实际上是一个虚拟设备)上的文件的MIME /媒体类型。我发现这个资源获取从文件的MIME类型,建议在的javax。 activati​​on.MimetypesFileTypeMap ,但是当我运行下面的code,我得到应用程序/八位字节流所有文件类型:

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

我已经测试了这个具有以下扩展名的文件:JPG格式PDF,TXT,DOC,XSLX和他们都返回了同样的事情。有什么我必须做的初始化地图?可这个库找不到在Android上支持的MIME类型的列表?有没有更好的方式来获得文件的MIME类型在Android?

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?

推荐答案

有这个问题为好。根据Java文档的MimetypesFileTypeMap中搜索

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

  1. 在编程方式添加的条目到MimetypesFileTypeMap中的实例。
  2. 在用户的主目录中的文件的.mime.types。
  3. 在该文件/lib/mime.types
  4. 的文件或资源名为META-INF / MIME类型
  5. 在该文件或文件名为META-INF / MIME types.default(通常只存在于activati​​on.jar文件中找到)资源

如果您的MIME类型都出来为应用程序/八位字节流这意味着你有没有上述文件present(或他们是present但不完全),并没有添加任何条目您MimetypesFileTypeMap中的实例。

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.

要解决...

MimetypesFileTypeMap中MIME类型规范格式

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!

这篇关于Java的MimetypesFileTypeMap中总是在Android模拟器返回应用程序/八位字节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆