【发布时间】:2018-01-29 23:48:54
【问题描述】:
当使用 ACTION_SEND Intent 共享未知文件类型时,设置内容类型时应该使用*/* 或application/octet-stream 吗?
根据Mozilla的Complete list of MIME types
两种主要的 MIME 类型对于默认类型的作用很重要:
- text/plain 是文本文件的默认值。文本文件应该是人类可读的,并且不得包含二进制数据。
- application/octet-stream 是所有其他情况的默认值。未知文件类型应使用此类型。浏览器在处理这些文件时会特别小心,试图保护用户以防止危险行为。
例子
Intent intent = new Intent(Intent.ActionSend);
Uri uri = Uri.FromFile(file);
intent.PutExtra(Intent.ExtraStream, uri);
string fileType = GetMimeTypeByUri(uri);
if (fileType == null)
{
fileType = "*/*"; // ?
fileType = "application/octet-stream"; // ?
fileType = "application/x-binary" // ?
}
intent.SetType(fileType);
StartActivity(Intent.CreateChooser(intent, "Send to..."));
在哪里
private String GetMimeTypeByUri(Uri uri)
{
if (uri.Scheme.Equals(ContentResolver.SchemeContent))
return ContentResolver.GetType(uri);
else
return Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(
Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(uri.Path).ToLower()
);
}
}
【问题讨论】:
-
参考这篇文章好像是的androidsbs.blogspot.com.tr/2014/01/…
-
您想使用合适的应用打开未知文件?
-
@YorkShen-MSFT ACTION_GET_CONTENT 用于“开放”。
-
您可以阅读 official documents :如果 MIME 类型未知,请使用
*/*。 -
@YorkShen-MSFT ...“这将只允许可以处理通用数据流的发件人”...我不确定那是什么,但我总是可以阅读源代码。
标签: android android-intent xamarin.android