Search code examples
androidcsvkotlinemail-attachments

Kotlin - CSV file sharing request contains no data


for my app i'm attempting to create a csv file and share it via email/google drive. however when i go to share the csv file, im am presented with the following "toast" . The code is located within a fragment.

upload was unsuccessful request contained no data

the following is my current code:

  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!")

i have tried following similar questions asked on this site, however none of them were able to help as they were not in kotlin or i was unable to interpret it for my example. The attempt i have in this code is the closest i have gotten.

i have also tried:

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

as it was a suggested answer for Kotlin Android create and share CSV file

Would Anyone have any ideas on how to fix this?


Solution

  • i got it to work by first adding the following code into the androidManifest.xml with in the section.

     <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>
    

    after this create an xml file in res>xml>provider_paths* inside the provider_paths.xml aded the following code

    <?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>
    

    finally to create the CSV file the code below was used inside the 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")
            }