调试时我的控制台日志显示没有错误,如果文件不存在,则正在创建文件,并且正在正确读取.然而事实并非如此.当我在我的设备上构建并运行时,不会保存数据.此外,在指定的位置也不会创建任何文件.
我控制台记录了它试图使用的路径,这就是它:
文件:///data/data/com.adobe.phonegap.app/files/calendar.txt
$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); } ); };
我已经用这个教程来实现这个目标:Cordova File Plugin Tutorial
我究竟做错了什么?