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

Kotlin - CSV file sharing request contains no data

对于我的应用程序,我正在尝试创建一个 csv 文件并通过 email/google 驱动器共享它。 然而,当我去分享 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!")

我曾尝试关注此站点上提出的类似问题,但其中 none 能够提供帮助,因为它们不在 kotlin 中,或者我无法为我的示例解释它。我在这段代码中的尝试是我最接近的尝试。

我也试过:

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

因为这是 Kotlin Android create and share CSV file

的建议答案

有没有人知道如何解决这个问题?

我通过首先将以下代码添加到 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("3")
                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")
        }