Search code examples
javascriptandroidfilecordovahybrid

Why isn't getFile() creating any files even though i have set the create flag on true?


I'm trying to write some data in a file and save it on the internal memory of a mobile device. I'm working on a cordova project which has the cordova-plugin-file installed. Everything seems to be working without errors, but no files are created, so no data is saved... Why isn't getFile() creating any files even though i have set the create flag on 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);
}

Solution

  • I have found a solution : using window.resolveLocalFileSystemURL instead of window.requestFileSystem. However I could only access the internal sd card using cordova.file.externalDataDirectory. just cordova.file.dataDirectory gives me a non-existing path file:///data/data/... The following code proves to be working for 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");
        });
    });