我正在寻找一种方法在 Cordova for Android 的内部存储上创建一个目录(不是 www
目录),这样我就有一个如下路径:
/mnt/sdcard/myfolder/
/sdcard/myfolder/
/storage/emulated/0/myfolder/
(这些路径在物理上是相同的)
我发现一些脚本在 www
目录中运行,但如何在内部存储上创建文件夹?
提前致谢!
最佳答案
此示例代码可让您在 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
}
关于javascript - 如何在内部存储上创建文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333118/