对于我的应用程序,我试图创建一个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的建议答案
有人知道怎么解决这个问题吗?
1条答案
按热度按时间daupos2t1#
我首先将以下代码添加到androidManifest.xml中,并在小节中添加了以下代码。
之后,在res〉xml〉provider_paths*中创建一个xml文件,该文件位于provider_paths.xml内,并添加以下代码
最后,为了创建CSV文件,在onClickListener中使用了以下代码。