如何在 intent android 中传递 2 个文件

how to pass 2 files in intent android

我需要分享我在应用程序中创建的 2 个 csv 文件。 我可以使用以下代码共享其中一个文件,但如何传递 2 个文件进行共享?

  for (File file : fileList) {

                    if (!file.exists()) {
                        Toast.makeText(ExcelExportActivity.this, "File doesn't exists", Toast.LENGTH_LONG).show();
                        return;
                    }
                    Uri uri = FileProvider.getUriForFile(ExcelExportActivity.this, "com.example.farmers.provider", file);

                    grantUriPermission(ExcelExportActivity.this.getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    sharingIntent.setType("text/csv");
                    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);


                }
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "my subject");
                startActivity(Intent.createChooser(sharingIntent, "sharing with..."));

您需要使用 Intent.ACTION_SEND_MULTIPLE 而不是 Intent.ACTION_SEND。

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("text/csv");

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

for(String path : filesToSend /* List of the files you want to send */) {
    File file = new File(path);
    Uri uri = Uri.fromFile(file);
    files.add(uri);
}

intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);