Android Kotlin将应用程序文件复制到;档案;应用程序通过意图
我有一个Android Kotlin应用程序。我使用的是API27+。我的应用程序中生成了一个.csv文件。我正在尝试将此应用程序创建的.csv文件写入android设备“文件”应用程序 我可以在我的应用程序的缓存目录中创建该文件。缓存文件看起来很棒 当我使用intent编写文件应用程序时,会创建一个“Files”文件,但它是空的 要将应用程序的.csv文件复制到包含数据的“文件”应用程序,我需要更改什么?如果创建文档是错误的意图,那么哪一个是正确的?如果我更改了intent类型,我需要对manifest/gradle进行哪些更改才能共享数据 在我的应用程序中创建虚拟.csv文件的代码:Android Kotlin将应用程序文件复制到;档案;应用程序通过意图,android,kotlin,android-intent,Android,Kotlin,Android Intent,我有一个Android Kotlin应用程序。我使用的是API27+。我的应用程序中生成了一个.csv文件。我正在尝试将此应用程序创建的.csv文件写入android设备“文件”应用程序 我可以在我的应用程序的缓存目录中创建该文件。缓存文件看起来很棒 当我使用intent编写文件应用程序时,会创建一个“Files”文件,但它是空的 要将应用程序的.csv文件复制到包含数据的“文件”应用程序,我需要更改什么?如果创建文档是错误的意图,那么哪一个是正确的?如果我更改了intent类型,我需要对man
private fun downloadFile() {
val CSV_HEADER = "id,name,address,age"
val myToyBoxFile = File.createTempFile("MyToyBox", ".csv")
var fileWriter: FileWriter? = null
try {
fileWriter = FileWriter(myToyBoxFile)
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()
}
fileWriter!!.close()
val uriForFile = Uri.fromFile(myToyBoxFile)
createFile(uriForFile)
}
意图代码
val CREATE_FILE = 1
private fun createFile(pickerInitialUri: Uri) {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
putExtra(Intent.EXTRA_STREAM, pickerInitialUri)
addCategory(Intent.CATEGORY_OPENABLE)
type = "text/csv"
putExtra(Intent.EXTRA_TITLE, "myToyBox.csv")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivityForResult(intent, CREATE_FILE)
}
我没有让“文件”应用程序的集成工作,但我确实可以访问“共享”工作,允许我通过电子邮件发送文件 Android页面及其链接以及许多stackoverflow页面是什么 高级别
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.com.YOURDOMAIN.YOURAPP.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_docs" path="docs/"/>
</paths>
val filePath: File = File(requireContext().filesDir, "docs")
filePath.mkdirs()
val newFile = File(filePath, "MyToyBox.csv")
newFile.delete()
newFile.createNewFile()
val contentUri = FileProvider.getUriForFile(
requireContext(),
"com.YOURDOMAIN.YOURAP.fileprovider",
newFile
)
val CSV_HEADER = "Series,My Toy Box ID,Debut Year,Phase,Wave,Action Figure Package Name," +
"Movie Scene," +
"Estimated New Value,Have the New Figure,Have New Count," +
"Want the New Figure,Want New Count," +
"Sell the New Figure,Sell New Count," +
"Order the New Figure,Order New Count," +
"Order from Text," +
"Have the Loose Figure,Have Loose Count" +
",Want the Loose Figure,Want Loose Count" +
",Sell the Loose Figure,Sell Loose Count" +
",Notes" + "\n"
var fileWriter: FileWriter? = null
try {
fileWriter = FileWriter(newFile)
fileWriter.append(CSV_HEADER)
figureList.forEach { figure ->
val specifics = figure.specifics
val noText = "No"
val yesText = "Yes"
val new_haveCount = specifics.new_haveCount.toString()
val new_wantCount = specifics.new_wantCount.toString()
val new_sellCount = specifics.new_sellCount.toString()
val new_orderCount = specifics.new_orderCount.toString()
val new_orderText = specifics.new_orderText
val loose_haveCount = specifics.loose_haveCount.toString()
val loose_wantCount = specifics.loose_wantCount.toString()
val loose_sellCount = specifics.loose_sellCount.toString()
// set yes/no text based on count
val new_haveTheFigure = if (specifics.new_haveCount == 0) noText else yesText
val new_wantTheFigure = if (specifics.new_wantCount == 0) noText else yesText
val new_sellTheFigure = if (specifics.new_sellCount == 0) noText else yesText
val new_orderTheFigure = if (specifics.new_orderCount == 0) noText else yesText
val loose_haveTheFigure = if (specifics.loose_haveCount == 0) noText else yesText
val loose_wantTheFigure = if (specifics.loose_wantCount == 0) noText else yesText
val loose_sellTheFigure = if (specifics.loose_sellCount == 0) noText else yesText
val notes = specifics.notes
// formatted value
var newValueString = kUnknownMTBValue
val currencyFormat = NumberFormat.getCurrencyInstance()
currencyFormat.maximumFractionDigits = 2
currencyFormat.currency = Currency.getInstance("USD")
if (figure.saleSummary != null) {
val formattedValue =
currencyFormat.format(figure.saleSummary!!.estimatedValueMean).toString()
newValueString =
context?.getString(R.string.saleSummaryValueCount,
formattedValue,
figure.saleSummary!!.saleCount).toString()
}
// need to escape , with \" in front and back, such as in wave and names.
val row: String = "\"${figure.series.seriesName}\"," +
"${figure.figureUniqueId}," +
"${figure.debutYear}," +
"\"${figure.phase}\"," +
"\"${figure.wave}\"," +
"\"${figure.figurePackageName}\"," +
"\"${figure.scene}\"," +
"\"$newValueString\"," +
"$new_haveTheFigure,$new_haveCount," +
"$new_wantTheFigure,$new_wantCount," +
"$new_sellTheFigure,$new_sellCount," +
"$new_orderTheFigure,$new_orderCount," +
"\"$new_orderText\"" +
",$loose_haveTheFigure,$loose_haveCount," +
"$loose_wantTheFigure,$loose_wantCount," +
"$loose_sellTheFigure,$loose_sellCount," +
"\"$notes\"" + "\n"
fileWriter.append(row)
}
} catch (e: Exception) {
println("Writing CSV error!")
e.printStackTrace()
}
fileWriter!!.close()
createFile(contentUri)
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, pickerInitialUri)
type = "text/plain"
setDataAndType(pickerInitialUri, requireContext().contentResolver.getType(pickerInitialUri))
}
startActivity(Intent.createChooser(shareIntent, "My Toy Box"))