Android Gmail 不通过 FileProvider 附加文​​件的意图 "Unable to attach file"

Android Intent to Gmail not attaching file by FileProvider "Unable to attach file"

将文件附加到 Gmail 时,我在附件中短暂地看到了该文件,然后 Toast 提示“无法附加文件”,然后它就消失了。它适用于 Drive、Discord 和其他应用程序。 该文件也保留在模拟器的附件中,但是当我发送它时,邮件发送时没有附件。我有一个简单的 .csv 文件并通过 FileProvider 附加它。

尝试写入内部存储,没有帮助。

val fileLocation = File(requireContext().getExternalFilesDir("data"), "data.csv")

            // Saving the file into device
            val streamOut =
                FileOutputStream(fileLocation)
            streamOut.write(myString.toByteArray())
            streamOut.close()

            // Exporting

            val contentUri = FileProvider.getUriForFile(
                requireContext(),
                "mypackage.fileprovider",
                fileLocation
            )

            val fileIntent = Intent(Intent.ACTION_SEND)
                .setType("text/csv")
                .putExtra(Intent.EXTRA_SUBJECT, "Data")
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                .putExtra(Intent.EXTRA_STREAM, contentUri)

            val chooser = Intent.createChooser(
                fileIntent,
                requireContext().resources.getText(R.string.send_to)
            )

            chooser.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

            val resInfoList: List<ResolveInfo> = requireActivity().packageManager
                .queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY)

            for (resolveInfo in resInfoList) {
                val packageName = resolveInfo.activityInfo.packageName
                requireActivity().grantUriPermission(
                    packageName,
                    contentUri,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
                )
            }

            requireActivity().startActivity(
                chooser
            )

provider_paths

<paths>
    <external-files-path
        name="data"
        path="." />
</paths>

清单

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="mypackage.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

您可以尝试更新您的 file_paths.xml 以使用外部路径的特定路径并尝试它是否有效?

请参阅下面我的工作解决方案。它使用多个附件,但适用于 Gmail 应用程序:

java class

private void prepareEmail(File report, List<Expense> openExpenses) {
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{preferences.getEmailReceiver()});
    intent.putExtra(Intent.EXTRA_SUBJECT, preferences.getEmailSubject());
    intent.putExtra(Intent.EXTRA_TEXT, preferences.getEmailBody());

    ArrayList<Uri> uris = new ArrayList<>();
    uris.add(FileProvider.getUriForFile(getApplicationContext(), "my.package", report));
    for (Expense expense : openExpenses) {
        if (expense.getType() == ExpenseType.EXPENSE.getValue()) {
            File file = new File(expense.getReceipt());
            uris.add(FileProvider.getUriForFile(getApplicationContext(), "my.package", file));
        }
    }
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    startActivity(Intent.createChooser(intent, getResources().getString(R.string.report_report_send)));
}

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/my.package/files/Pictures" />
    <external-path name="my_pdfs" path="Android/data/my.package/files/Documents" />
    <external-path name="my_reports" path="Android/data/my.package/files" />
    <files-path name="files" path="." />
</paths>

清单

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="my.package"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

通过根据此模板更改file_paths.xml解决了问题:

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-path
    name="external"
    path="." />
  <external-files-path
    name="external_files"
    path="." />
  <cache-path
    name="cache"
    path="." />
  <external-cache-path
    name="external_cache"
    path="." />
<files-path
    name="files"
    path="." />
</paths>