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

标签 android csv kotlin share

我的 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/

相关文章:

json - 使用 Jackson 序列化(反)序列化 Kotlin 委托(delegate)属性

kotlin - 无法在 kotlin 中将 AsyncTask 转换为 Retrofit2

java - Android 在 webview 中创建带有链接的 toast

android - 如何创建包含两边带有按钮的 TextView 的布局

android - TrafficStats.getMobileRxBytes() 和 TrafficStats.getMobileTxBytes() 在 Nexus 5x 中总是返回 0

java - 类型转换后的比较

reactjs - CSVLink 在 `react-csv` npm 包中生成 HTML 而不是 CSV

python - 使用 Pandas 或替代方法比较大量 (~40GB) 文本数据

testing - Kotlin 测试 DSL 中的限制

android - 在 TextView 中指定每行中的字符数?