gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:16:02 27 4
gpt4 key购买 nike

将文件附加到 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>

list

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

关于Android Intent to Gmail 不通过 FileProvider 附加文​​件 "Unable to attach file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62619201/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com