Java MimetypesFileTypeMap 总是在 Android 模拟器上返回应用程序/八位字节流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4855627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java MimetypesFileTypeMap always returning application/octet-stream on Android emulator
提问by Ben
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 Filewhich recommends the javax.activation.MimetypesFileTypeMap
, however when I run the following code I get application/octet-stream
for all file types:
我正在尝试确定存储在 Android 设备(实际上是我与模拟器一起使用的虚拟设备)上的文件的 Mime/Media 类型。我发现这个资源从推荐的文件中获取 MIME 类型javax.activation.MimetypesFileTypeMap
,但是当我运行以下代码时,我得到application/octet-stream
了所有文件类型:
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?
我已经使用具有以下扩展名的文件对此进行了测试:jpg、txt、doc、xslx 和 pdf,它们都返回相同的内容。我需要做些什么来初始化地图吗?这个库在 Android 上找不到支持的 MIME 类型列表吗?有没有更好的方法可以在 Android 上获取文件的 MIME 类型?
回答by HipsterCarlGoldstein
Had this problem as well. According to the Java Docs the MimetypesFileTypeMap searches
也有这个问题。根据 Java Docs MimetypesFileTypeMap 搜索
- Programmatically added entries to the MimetypesFileTypeMap instance.
- The file .mime.types in the user's home directory.
- The file /lib/mime.types
- The file or resources named META-INF/mime-types
- The file or resources named META-INF/mime-types.default (usually only found in the activation.jar file)
- 以编程方式向 MimetypesFileTypeMap 实例添加条目。
- 用户主目录中的文件 .mime.types。
- 文件 /lib/mime.types
- 名为 META-INF/mime-types 的文件或资源
- 名为 META-INF/mime-types.default 的文件或资源(通常只能在 activation.jar 文件中找到)
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.
如果您的 Mimetypes 都作为“application/octet-stream”出现,这意味着您没有上述文件(或者它们存在但不完整)并且没有向您的 MimetypesFileTypeMap 实例添加任何条目。
To solve...
为了解决...
MimetypesFileTypeMap mimetypes specification format
MimetypesFileTypeMap mimetypes 规范格式
# 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!