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

How to create a folder on the internal storage?

本文关键字:创建 文件夹 存储器 在内部      更新时间:2023-09-26

我正在寻找一种方法,在Cordova中为Android在内部存储(不是www dir)创建一个目录,这样我就有一个路径:

  • /mnt/sdcard/myfolder/
  • /sdcard/myfolder/
  • /storage/emulated/0/myfolder/

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

我发现一些脚本在www目录内工作,但我如何在内部存储上创建一个文件夹?

提前感谢!

这个示例代码允许你在Android的外部根目录和iOS的documents文件夹中创建一个文件夹:

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
}