我正在开发一个需要编写 .xml
文件的应用程序。现在文件只有在重新启动 Android 设备
后才会被填充,我不明白为什么。这是我的代码:
override fun doInBackground(vararg params: Void?): Boolean {
val sagaCollectFolder = File(Environment.getExternalStorageDirectory(), ".sagacollect")
if (!sagaCollectFolder.exists())
sagaCollectFolder.mkdir()
try {
val file = File(
sagaCollectFolder,
"collect.xml"
)
file.createNewFile()
val factory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance()
val builder: DocumentBuilder = factory.newDocumentBuilder()
val doc: Document = builder.parse(InputSource(StringReader(JRPresenter.getFilledFormXml().toString())))
// Write the parsed document to an xml file
val transformerFactory: TransformerFactory = TransformerFactory.newInstance()
val transformer: Transformer = transformerFactory.newTransformer()
val source = DOMSource(doc)
val result = StreamResult(file)
transformer.transform(source, result)
} catch (exception: Exception) {
Log.e(TAG, exception.toString())
}
return true
}
这是我执行 AsyncTask
的地方:
fun saveResult(index: FormIndex, answer: IAnswerData?) {
formController.saveAnswer(index, answer)
WriteCacheAsync().execute()
}
任何人都知道我应该更改什么以便在调用方法后而不是在重启设备后填充文件?
干杯,
毛里西奥
最佳答案
@blackapps 解决了我的问题:
Wrong method. Because Windows depends on the media store of the Android device and you did not inform the media store about the new file. Indeed the media store scanner will find your file after reboot. Use a file manager app on the device instead. Or check self with code
使用文件管理器,我发现一切正常。非常感谢大家。
关于android - 文件仅在重启 Android 设备后写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749604/