我觉得下面这行可能是问题所在,
"window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successCallback, errorCallback)"
在我阅读的一些帖子中,有人提到 requestfilsystem 方法和 LocalFileSystem.PERSISTENT 参数在 android 中不起作用,除非设备是 root 的。
我使用 - "window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,successCallback, errorCallback);"
使它工作
如果需要,我可以分享删除目录的示例代码以及其中的文件。请告诉我。希望对您有所帮助。
这是根据请求的示例代码,
function clearDirectory() {
if (sessionStorage.platform.toLowerCase() == "android") {
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemDirSuccess, fail);
} else {
//for ios
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemDirSuccess, fail);
}
};
function onFileSystemDirSuccess(fileSystem) {
var entry = "";
if (sessionStorage.platform.toLowerCase() == "android") {
entry = fileSystem;
} else {
//for ios
entry = fileSystem.root;
}
entry.getDirectory("Folder_Name", {
create: true,
exclusive: false
},
function(entry) {
entry.removeRecursively(function() {
console.log("Delete successful !!!");
}, fail);
}, getDirFail);
};
function getDirFail(error) {
alert("getDirFail - " + error.code);
};
function fail(error) {
alert("fail - " + error.code);
};
文件创建:
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
}