为什么我不能在 Android N+ 中附加文件?

Why cannot I attach a file in Android N+?

我正在开发一个将一些 HTML table 导出为附件的应用程序。我注意到如果我尝试将 HTML table 附加到 Gmail、Google 驱动器或 Android N+ 中的任何电子邮件提供商,我的代码将不起作用,但它可以将文件上传到 OneDrive、WhatsApp、Skype 等,没有任何问题。

这是我当前的代码:

Intent share = new Intent(Intent.ActionSend);
share.SetType("text/html");
share.AddFlags(ActivityFlags.NewDocument);

var file = CreateDirFile($"Meeting_{DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss")}.html");

try
{
    FileOutputStream fout = new FileOutputStream(file);
    fout.Write(System.Text.Encoding.ASCII.GetBytes($"<!DOCTYPE html><html><body><table><tr><td>1</td><td>2</td><td>3</td></tr></table></body></html>"));
    fout.Close();
}
catch
{
    Toast.MakeText(context, context.GetString("Please check your storage configuration.").Show();
}

if (Build.VERSION.SdkInt < BuildVersionCodes.N)
{
    share.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(file.AbsoluteFile));
}
else
{
    share.AddFlags(ActivityFlags.GrantReadUriPermission);
    share.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse(file.Path));
}
share.PutExtra(Intent.ExtraSubject, $"Meeting {DateTime.Now.ToString("dd-MM-yyyy")}");
context.StartActivity(Intent.CreateChooser(share, "Email:"));

CreateDirFile 函数:

private Java.IO.File CreateDirFile(string fileName)
{
    string root = null;
    if (Android.OS.Environment.IsExternalStorageEmulated)
    {
        root = Android.OS.Environment.ExternalStorageDirectory.ToString();
    }
    else
    {
        root = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
    }

    Java.IO.File myDir = new Java.IO.File($"{root}/Meetings");
    myDir.Mkdir();

    Java.IO.File file = new Java.IO.File(myDir, fileName);

    if (file.Exists())
    {
        file.Delete();
        file.CreateNewFile();
    }

    return file;
}

我已经测试了很多组合,甚至应用了以下代码 Android.Net.Uri.Parse(file.Path),正如 SO 或其他论坛的不同答案中所建议的那样,但它没有按预期工作。

你们中有人遇到过类似的问题吗?你知道我应该改变什么吗?提前致谢。

未能尝试此代码,但它应该可以工作。至少它对我有用很多次。

在您的 onCreate 方法中尽早添加此代码。

if(Build.VERSION.SDK_INT >= 24){
    try{
        Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
        m.invoke(null);
    } catch (Exception e){
        e.printStackTrace();
    }
}

此代码的作用是禁用 android api 24 及更高版本中的严格模式文件 uri 规则。此代码用于强制将 FileProvider 用于打算通过 public URI 对象共享包含文件的对象的应用程序。事实证明,大多数时间和其他许多时间一样,这是不必要的。

尽管 Richard 在 Java 中的回答是正确的,但 C# 中需要进行一些更改,如下所示:

OnCreate事件中:

if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
    StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
                        .PenaltyDeathOnFileUriExposure()
                        .Build();
    StrictMode.SetVmPolicy(policy);
}

此外,您只需要这样做:

share.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(file.AbsoluteFile));

您不需要验证 OS 版本,并且此代码 Android.Net.Uri.Parse(file.Path)) 必须删除 以实现正确的行为。