gpt4 book ai didi

android - Kotlin Android 创建和共享 CSV 文件

转载 作者:行者123 更新时间:2023-11-29 23:46:57 24 4
gpt4 key购买 nike

我的 android 应用程序创建一个 csv 文件并立即将其共享为以下代码。该应用程序正常启动,但当我选择任何应用程序来共享它时,没有附加文件。请帮助解决这个问题。

class MainActivity : AppCompatActivity() {

private val CSV_HEADER = "id,name,address,age"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


val qponFile = File.createTempFile("qpon", "csv")
var fileWriter: FileWriter? = null

try {
fileWriter = FileWriter("qpon.csv")

fileWriter.append(CSV_HEADER)
fileWriter.append('\n')


fileWriter.append("aaaaa")
fileWriter.append(',')
fileWriter.append("bbbbb")
fileWriter.append(',')
fileWriter.append("cccccc")
fileWriter.append(',')
fileWriter.append("dddddd")
fileWriter.append('\n')

println("Write CSV successfully!")

} catch (e: Exception) {
println("Writing CSV error!")
e.printStackTrace()
}


val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_STREAM, qponFile)
sendIntent.type = "text/csv"
startActivity(Intent.createChooser(sendIntent, "SHARE"))

}

}

Image1,应用正常启动。 enter image description here

图2,无附件 enter image description here

最佳答案

写入文件后必须关闭它:
fileWriter.close()
并进行此更改:
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(qponFile))

关于android - Kotlin Android 创建和共享 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51248872/

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