【问题标题】:ACTION_SEND Intent with unknown content type (*/* vs application/octet-stream)ACTION_SEND Intent 具有未知的内容类型(*/* vs application/octet-stream)
【发布时间】:2018-01-29 23:48:54
【问题描述】:

当使用 ACTION_SEND Intent 共享未知文件类型时,设置内容类型时应该使用*/*application/octet-stream 吗?

根据MozillaComplete 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


【解决方案1】:

fileType = "* / *";

如果您在无法从系统中确定它时使用“* / *”的 MIME 类型(它为空),则会触发相应的选择应用程序对话框。

fileType = "应用程序/八位字节流";

通常,它是必须在应用程序(例如电子表格或文字处理器)中打开的应用程序或文档。 The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data

fileType = "应用程序/x-binary"

这只是重述“八位字节流”的一种非标准方式。

查看链接了解详情 MIME types , MIME Utils

希望这对你有帮助。

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 2012-05-06
    • 2019-10-16
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多