嵌套目录创建者:Phonegap

时间:2012-06-09 12:28:20

标签: android cordova directory

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

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

我在Android 2.2中使用Phonegap 1.8.0。

5 个答案:

答案 0 :(得分: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());
}

有关完整示例,请查看此gist

答案 1 :(得分:2)

只是为dhaval的功能添加一些东西:它不是任何符合Javascript Filesystem的浏览器的防弹。如果你在Chrome中使用它并且你的路径是一个空字符串,它将会失败,因为很明显,对于Chrome,在空字符串上使用split()会返回一个元素数组,一个元素本身是一个epmty字符串,然后会导致目录创建失败。我修改了它以纠正问题(还有其他一些不相关的更改,为了我自己的目的):

function createPath(fs, path, callback) {
    var dirs = path.split("/").reverse();
    var root = fs.root;

    var createDir = function(dir) {
        if (dir.trim()!="") {
            root.getDirectory(dir, {
                create: true,
                exclusive: false
            }, success, function(dir) {
                error("failed to create dir " + dir);
            });
        } else {
            callback();
        }
    };

    var success = function(entry) {
        root = entry;
        if (dirs.length > 0) {
            createDir(dirs.pop());
        } else {
            callback();
        }
    };

    createDir(dirs.pop());
}

答案 2 :(得分:1)

此代码应该按您的要求执行:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(filesys) {
    filesys.root.getDirectory("story_repository", {create: true, exclusive: false}, function(dirEntry) {
        dirEntry.getDirectory("dir_name", {create: true, exclusive: false}, function(dirName) {
            dirName.getDirectory("img", {create: true, exclusive: false}, function(imgDir) {
                console.log("img creation worked");
            }, function(error) {
                console.log("create img failed");
            });
            dirName.getDirectory("res", {create: true, exclusive: false}, function(imgDir) {
                console.log("res creation worked");
            }, function(error) {
                console.log("create res failed");
            });
        }, function(error) {
            console.log("create dir_name failed");
        })
    }, function(error) {
        console.log("create story repository failed");
    });
}, function(error) {
    console.log("request file system failed");
});

答案 3 :(得分:1)

我正在使用它:

function recursiveGetFile(root, path, opts, success, fail) {
    function dir(entry) {
        var name = path.shift();
        if (path.length > 0)
            entry.getDirectory(name, opts, dir, fail);
        else
            entry.getFile(name, opts, success, fail);
    }
    path = path.split('/');
    dir(root);
}, fail);

答案 4 :(得分:1)

有一个简单的cordova-phoengap文件管理器,你可以做到这一点以及更多:

https://github.com/torrmal/cordova-simplefilemanagement

您可以递归创建目录:

//CREATE A DIRECTORY RECURSIVELY as simple as:

new DirManager().create_r('folder_a/folder_b',Log('created successfully'));

如果有帮助请告诉我