我在 Android (Kotlin) 中共享 .txt 文件时遇到问题。共享简单文本不是问题。但我需要通过蓝牙、Gmail 等共享这个文件。 每次 G-mail 返回:“无法附加文件”。
我的分享功能:
fun shareFile(file:File){
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.setType("text/*")
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.absolutePath))
startActivity(Intent.createChooser(sharingIntent, "share file with:"))
}
我已阅读此站点中的所有主题,但没有任何效果:-/
最佳答案
终于我找到了解决问题的方法,我将其发布在这里以供遇到相同问题的任何人使用:
if (file.exists()) {
val uri = FileProvider.getUriForFile(
this,
BuildConfig.APPLICATION_ID + ".provider",
file
)
val intent = Intent(Intent.ACTION_SEND)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.setType("*/*")
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent)
}
关于android - 如何在 kotlin android 中共享 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70520929/