如何在内部存储上创建文件夹?

时间:2016-09-05 14:49:55

标签: javascript android cordova

我正在搜索在内部存储(而不是replace() dir)上在Cordova for Android中创建目录的方法,这样我就有了这样的路径:

  • www
  • /mnt/sdcard/myfolder/
  • /sdcard/myfolder/

(这些路径在物理上是相同的)

我在/storage/emulated/0/myfolder/目录中找到了一些脚本,但是如何在内部存储上创建文件夹?

提前致谢!

1 个答案:

答案 0 :(得分:1)

此示例代码允许您在Android的外部根目录和iOS中的文档文件夹中创建文件夹:

function writeFile() {
        if (sessionStorage.platform.toLowerCase() == "android") {
            window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
        } else {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
        }
}    

function onError(e) {
    alert("onError");
};

function onFileSystemSuccess(fileSystem) {
    var entry = "";
    if (sessionStorage.platform.toLowerCase() == "android") {
        entry = fileSystem;
    } else {
        entry = fileSystem.root;
    }
    entry.getDirectory("Folder_Name", {
        create: true,
        exclusive: false
    }, onGetDirectorySuccess, onGetDirectoryFail);
};

function onGetDirectorySuccess(dir) {
    dir.getFile(filename, {
        create: true,
        exclusive: false
    }, gotFileEntry, errorHandler);
};

function gotFileEntry(fileEntry) {
    // logic to write file in respective directory
};

function errorHandler(e) {
    // handle error
}