Search code examples
cordovacordova-plugins

Cordova file is not creating directory


I have a cordova application that uses the file plugin, inside the app everything works fine, but the files that i use doesn't appear in File Explorer.

My class is:

function FileCordova () {

    this.createDir = function(dirName, callback) {

      App.db.fsSetup();
      if (App.db.hasFS()) {

            window.requestFileSystem(window.PERSISTENT, 0, function (f) {
              f.root.getDirectory(dirName, {create: true}, function (d) {
                  typeof callback === 'function' && callback(d.name, d.fullPath);
              },
              function(error, err2){
                console.log(error);
              });
            }, function(error,err2){
              console.log(error);
            });

        } else {
            typeof callback === 'function' && callback();
        }
    };

}

Inside the app de file appear, but I can't find in the root directory.


Solution

  • I just recently had to do this in my app. I used the window.resolveLocalFileSystemURL() method instead of window.requestFileSystem():

    var exportDirectory = "";
    var subdir = "MySubdirectory";
    // What directory should we place this in?
    if (cordova.file.documentsDirectory !== null) {
        // iOS, OSX
        exportDirectory = cordova.file.documentsDirectory;
    } else if (cordova.file.sharedDirectory !== null) {
        // BB10
        exportDirectory = cordova.file.sharedDirectory;
    } else if (cordova.file.externalRootDirectory !== null) {
        // Android, BB10
        exportDirectory = cordova.file.externalRootDirectory;
    } else {
        // iOS, Android, BlackBerry 10, windows
        exportDirectory = cordova.file.DataDirectory;
    }
    
    window.resolveLocalFileSystemURL(exportDirectory, function (directoryEntry) {
        console.log("Got directoryEntry. Attempting to open / create subdirectory:" + subdir);
        directoryEntry.getDirectory(subdir, {create: true}, function (subdirEntry) {
            subdirEntry.getFile(filename, {create: true}, function (fileEntry) {
                console.log("Got fileEntry for: " + filename);
                fileEntry.createWriter(function (fileWriter) {
                    console.log("Got fileWriter");
                    // make your callback to write to the file here...
                }, exportFail);
            }, exportFail);
        }, exportFail);
    }, exportFail);
    

    Note, however, that the Android File Transfer utility did NOT refresh properly. I had to disconnect / reconnect my USB cable, and then the directory showed up. This might actually be your issue with File Explorer (i.e., check that before changing code).