javascript - 为什么 getFile() 不创建任何文件,即使我已将创建标志设置为 true?

标签 javascript android file cordova hybrid

我正在尝试将一些数据写入文件并将其保存在移动设备的内部存储器中。我正在开发一个安装了 cordova-plugin-file 的 cordova 项目。一切似乎都没有错误地工作,但是没有创建文件,所以没有保存数据...... 为什么 getFile() 没有创建任何文件,即使我已将创建标志设置为 true?

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);

function success(fileSystem) {
    window.rootFS = fileSystem.root;
    window.rootFS.getFile("TestFile.txt", { create:true }, onSuccess, onFail); 
    function onSuccess() {
        //write something on the file 
    }
    function onFail(error) {
        //show error
    }

function fail(error) {
    alert("Failed to retrieve file: " + error.code);
}

最佳答案

我找到了一个解决方案:使用 window.resolveLocalFileSystemURL 而不是 window.requestFileSystem。但是我只能使用 cordova.file.externalDataDirectory 访问内部 SD 卡。只是 cordova.file.dataDirectory 给了我一个不存在的路径 file:///data/data/... 以下代码证明适用于 Android。

window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) {
    console.log("got main dir",dir);
    dir.getFile("log.txt", {create:true}, function(file) {
        console.log("got the file", file);
        logOb = file;
        writeLog("App started");
    });
});

关于javascript - 为什么 getFile() 不创建任何文件,即使我已将创建标志设置为 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829408/

相关文章:

android - 如何在Android中获取一系列字符串?

c - 从txt文件中读取链表

c# - 复制文件集时出错

Javascript 基本执行顺序

javascript - 在函数之外使用 Angular var

javascript - 滚动速度操纵CSS jquery

javascript - 为什么球在我的 pong JavaScript Canvas 游戏中不能完全弹跳?

调用 Android Phonegap : Google Map marker not immediately removed when marker. setMap(null)

java - 如何访问同一包但不同 jar 中父类的包本地字段?

javascript - Jquery 文件树 - 如何在文件夹单击时返回文件夹名称