android - 文件仅在重启 Android 设备后写入

标签 android kotlin

我正在开发一个需要编写 .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/

相关文章:

android - 获取 3D Android 设备方向

Kotlin Context 具有高阶函数?

android - 为什么支持FragmentManager.saveFragmentInstanceState(selectedFragment)!! = 空

java - 在没有 kotlin 代码的情况下,我真的需要使用 kapt 吗?

android - Android Kotlin协同例程异常

android - 重复调用 LineBackgroundSpan drawBackground()

android - GCMBaseIntentService 回调仅在根包中

android - Gmail 错误 "Unable to attach file"

android - 我想从我的 flutter 应用程序启动 WhatsApp 应用程序

android - 下拉菜单(Spinner)未显示在 Android 的设计窗口中