Kotlin- CSV文件共享请求不包含数据

wxclj1h5  于 2023-01-06  发布在  Kotlin
关注(0)|答案(1)|浏览(111)

对于我的应用程序,我试图创建一个csv文件并通过电子邮件/谷歌驱动器共享它。然而,当我去共享csv文件时,即时通讯工具会显示以下“吐司”。该代码位于一个片段中。

upload was unsuccessful request contained no data

下面是我目前的代码:

val HEADER = "ID, Pa, m/s, Actual L/s, Design Pa, Design L/s, Design %"
        var fileName = "file://" + Environment.getExternalStorageDirectory() + "/Folder" + "/" + "mycsv.csv"
        println(" Debug: pressed   successfully!")

        var path = activity!!.getExternalFilesDir(null)   //get file directory for this package
        //(Android/data/.../files | ... is your app package)
        println(" Debug: path successfully!")
        //create fileOut object
        var fileOut = File(path, "mycsv.csv")
        println(" Debug: file   successfully!")
        //delete any file object with path and filename that already exists
        fileOut.delete()
        println(" Debug: deleted   successfully!")
        //create a new file
        fileOut.createNewFile()
        println(" Debug: file created  successfully!")
        //append the header and a newline
        fileOut.writeText(HEADER)
        fileOut.writeText("\n")
        // trying to append some data into csv file

        println(" Debug: csv written  successfully!")
        println("Debug:$fileOut")

        val sendIntent = Intent(Intent.ACTION_SEND)
        sendIntent.putExtra(Intent.EXTRA_STREAM, fileName)
        sendIntent.type = "text/csv"
        startActivity(Intent.createChooser(sendIntent, "Share File"))

        println(" Debug: sent page open successfully!")

我试过以下类似的问题问这个网站上,但他们都不能帮助,因为他们不在Kotlin或我无法解释它为我的例子.尝试我在这个代码是最接近我得到.
我也曾尝试:

sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(FileName))

因为这是Kotlin Android create and share CSV file的建议答案
有人知道怎么解决这个问题吗?

daupos2t

daupos2t1#

我首先将以下代码添加到androidManifest.xml中,并在小节中添加了以下代码。

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.YOUR_PACKAGE_NAME.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

之后,在res〉xml〉provider_paths*中创建一个xml文件,该文件位于provider_paths.xml内,并添加以下代码

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path
        name="files"
        path="."/>

    <external-path
        name="external_files"
        path="."/>
</paths>

最后,为了创建CSV文件,在onClickListener中使用了以下代码。

val csv_header = "ID, Pa, m/s, Actual L/s, DesignPa, Design L/s, Design %"

        var filename = "export.csv"

            var path = context!!.filesDir.absolutePath//get file directory for this package
//(Android/data/.../files | ... is your app package)
//create fileOut object
            var fileOut = File(path, filename)
//delete any file object with path and filename that already exists
            fileOut.delete()
//create a new file
            fileOut.createNewFile()
//append the header and a newline
            fileOut.appendText(csv_header)
            fileOut.appendText("\n")
// trying to append some data into csv file

                fileOut.appendText("$123")
                fileOut.appendText(",")
                fileOut.appendText("456")
              

        
            println("debug: Write CSV successfully!")

        if(fileOut.exists())
        try {
            val uri = FileProvider.getUriForFile(context!!, BuildConfig.APPLICATION_ID + ".provider", fileOut)
            val intent = Intent(Intent.ACTION_SEND)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.type = "text/csv"
            intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
            startActivity(Intent.createChooser(intent, "Share File"))
            println(" Debug1: sent page open successfully!")

        }catch (e: java.lang.Exception) {
            e.printStackTrace()
            println("debug: failed")
        }

相关问题