簡體   English   中英

Android 意圖 Gmail 未通過 FileProvider “無法附加文件”附加文件

[英]Android Intent to Gmail not attaching file by FileProvider “Unable to attach file”

將文件附加到 Gmail 時,我在附件中短暫看到了該文件,然后 Toast 說“無法附加文件”,然后它就消失了。 它適用於 Drive、Discord 和其他應用程序。文件也保留在模擬器上的附件中,但是當我發送它時,發送的郵件沒有附件。 我有一個 simple.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
            )

提供者路徑

<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來解決它:

<?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>

您可以嘗試更新您的 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)));
}

文件路徑.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM