Angularjs Cordova文件插件的目录错误
因此,我正在angularjs 1中构建一个cordova/phonegap应用程序,我试图保存并读取应用程序私有目录/沙盒中名为calendar.txt的文件,但无法保存 调试时我的控制台日志显示没有错误,如果文件不存在,则正在创建该文件,并且正在正确读取该文件。但事实并非如此。在设备上构建和运行时,数据不会保存。在指定的位置也没有创建任何文件 我记录了它试图使用的路径,如下所示: file:///data/data/com.adobe.phonegap.app/files/calendar.txt 以下是我用来打开文件的代码:Angularjs Cordova文件插件的目录错误,angularjs,cordova,cordova-plugins,Angularjs,Cordova,Cordova Plugins,因此,我正在angularjs 1中构建一个cordova/phonegap应用程序,我试图保存并读取应用程序私有目录/沙盒中名为calendar.txt的文件,但无法保存 调试时我的控制台日志显示没有错误,如果文件不存在,则正在创建该文件,并且正在正确读取该文件。但事实并非如此。在设备上构建和运行时,数据不会保存。在指定的位置也没有创建任何文件 我记录了它试图使用的路径,如下所示: file:///data/data/com.adobe.phonegap.app/files/calendar.
$rootScope.openFile = function(){
var pathToFile = cordova.file.dataDirectory + "calendar.txt";
console.log('path = ' + pathToFile);
window.resolveLocalFileSystemURL(pathToFile,
function(fileEntry){
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
$rootScope.calendar = JSON.parse(this.result);
console.log('file opened');
console.log(JSON.parse(this.result));
};
reader.readAsText(file);
}, function(error){});
}, function(error){
if(error.code == FileError.NOT_FOUND_ERR){
$rootScope.calendar = new Year();
console.log('no file found so it was created');
$rootScope.saveFile();
}
else{
console.log(error);
}
});
};
下面是我保存文件的代码:
$rootScope.saveFile = function(){
var data = JSON.stringify($rootScope.calendar, null, '\t');
var fileName = "calendar.txt"
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
function(directoryEntry){
directoryEntry.getFile(fileName, { create: true },
function (fileEntry) {
fileEntry.createWriter(
function (fileWriter) {
var blob = new Blob([data], { type: 'text/plain' });
fileWriter.write(blob);
console.log('file saved');
},
function (error){});
},
function (error){}
);
},
function(error){
console.log("Saving Error: Error during finding directory", error.message);
}
);
};
我使用本教程取得了以下成果:
我做错了什么 我假设你在安卓系统中面临这个问题,我也面临同样的问题。根据我的理解和谷歌搜索,在安卓系统中(至少在安卓5中),你不能在应用程序数据目录中写入,除非手机是根目录。因此,您可能必须改用externalRootDirectory 例如:window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,successCallback,errorCallback)强> 希望能有帮助