嵌套目录创建者:Phonegap

Huz*_*Bux 8 directory android cordova

如何使用此API在Phonegap中创建嵌套目录?

fileSystem.root.getDirectory("Android/data/com.phonegap.myapp/dir_one/dir_two/", {create:true}, gotDir, onError);
Run Code Online (Sandbox Code Playgroud)

我在Android 2.2中使用Phonegap 1.8.0.

dha*_*val 15

此功能将帮助您创建嵌套目录.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log("device is ready");
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function fail() {
    console.log("failed to get filesystem");
}

function gotFS(fileSystem) {
    window.FS = fileSystem;

    var printDirPath = function(entry){
        console.log("Dir path - " + entry.fullPath);
    }

    createDirectory("dhaval/android/apps", printDirPath);
    createDirectory("this/is/nested/dir", printDirPath);
    createDirectory("simple_dir", printDirPath);
}

function createDirectory(path, success){
    var dirs = path.split("/").reverse();
    var root = window.FS.root;

    var createDir = function(dir){
        console.log("create dir " + dir);
        root.getDirectory(dir, {
            create : true,
            exclusive : false
        }, successCB, failCB);
    };

    var successCB = function(entry){
        console.log("dir created " + entry.fullPath);
        root = entry;
        if(dirs.length > 0){
            createDir(dirs.pop());
        }else{
            console.log("all dir created");
            success(entry);
        }
    };

    var failCB = function(){
        console.log("failed to create dir " + dir);
    };

    createDir(dirs.pop());
}
Run Code Online (Sandbox Code Playgroud)

有关完整示例,请查看此要点