Android 使用文件提供程序发送包含多个附件的电子邮件

Android send email with multiple attachment using file provider

Android 应用程序在使用文件提供程序发送包含多个附件的电子邮件时出现问题。

我使用的是 intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+csvFilePath));,发送单个附件文件没有问题。然后我需要发送多个附件。我无法让它正常工作。

在我的 AndroidManifest.xml 中,我使用以下代码指定提供程序:

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

这是我的xml/provider_paths

 <?xml version="1.0" encoding="utf-8"?>
    <paths>
    <external-path
        name="myfiles"
        path="Android/data/com.example.abc/files/Documents"/>
    </paths>

发送邮件代码和文件路径为:

csvFilePath : /storage/emulated/0/Android/data/com.example.abc/files/Documents/Test123.csv
       xyzFilePath : /storage/emulated/0/Android/data/com.example.abc/files/Documents/xyz123.txt
     //attach multiple file
        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);

        intent.setType("text/plain");
        ArrayList<Uri> uris = new ArrayList<Uri>();

        //uris.add(Uri.fromFile(new File(csvFilePath)));
        //uris.add(Uri.fromFile(new File(xyzFilePath)));

        // using file provider

        File csvFile = new File(csvFilePath);
        File xyzFile = new File(xyzFilePath);
        uris.add(FileProvider.getUriForFile(MainActivity.this,
            BuildConfig.APPLICATION_ID + ".provider", csvFile ));
        uris.add(FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", xyzFile ));

        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

        intent.setData(Uri.parse("mailto:" + abc@xyz.com));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);


        startActivity(intent);

发送期间出现异常 mailandroid.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND_MULTIPLE dat=mailto:xxx.xxxxxxxx@xxxxx.xxx flg=0x10000001 clip={null U:content://com.example.abc.provider/myfiles/Test123.csv ...} (has extras) }

您的手机可能禁用了 gmail 或邮件发送应用程序。请检查应用程序是否已启用。 这个ActivityNotFoundException主要发生在没有应用处理intent的时候。

使用以下代码找到解决方案:

 xml/provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

File baseDir = new File(Environment.getExternalStoragePublicDirectory  (Environment.DIRECTORY_DOCUMENTS), "ABC");


 if (!baseDir.exists()) {
    baseDir.mkdirs();
 }

 Log.i("Debug", "baseDir " + baseDir.toString());
            File f1 = new File(baseDir, "File1.txt");
            writeToFile(f1, "This is file 1 contents 888888888");
            Log.i("Debug", "f1 path" + f1.getAbsolutePath());
            File f2 = new File(baseDir, "File2.txt");
            writeToFile(f2, "This is file 2 contents 123456");
            Log.i("Debug", "f2 path" + f2.getAbsolutePath());


            String f1path = f1.toString();
            String f2path = f2.toString();
            string EXTRA_RECIPIENT = "janedoe@abc.com";
            String message = "Test message 12345678 this is a test. ";
            sendMail(f1path, f2path, message, EXTRA_RECIPIENT);


 private void sendMail(String f1path, String f2path, String message, String mailTo) {
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple attachments");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo});

    File f1new = new File(f1path);
    File f2new = new File(f2path);

    ArrayList<Uri> uris = new ArrayList<Uri>();

    uris.add(FileProvider.getUriForFile(MainActivity.this,
            BuildConfig.APPLICATION_ID + ".provider", f1new ));
    uris.add(FileProvider.getUriForFile(MainActivity.this,
            BuildConfig.APPLICATION_ID + ".provider", f2new ));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra(Intent.EXTRA_TEXT, message);
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    startActivity(intent);
}

Then I need to send multiple attachments. I have a problem to get it working.

多个附件的示例:

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple");
intent.putExtra(Intent.EXTRA_TEXT, "multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(new File("/path/to/first/file")));
uris.add(Uri.fromFile(new File("/path/to/second/file")));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

调用 startActivity() 传递 intent。