我的 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"))
}
}
最佳答案
写入文件后必须关闭它:
fileWriter.close()
并进行此更改:sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(qponFile))
关于android - Kotlin Android 创建和共享 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51248872/