angularjs – Cordova文件插件具有错误的目录

所以我正在angularjs 1中构建一个cordova / phonegap应用程序,我正在尝试保存并从应用程序的私人目录/沙箱中名为calendar.txt的文件中读取而不能.

调试时我的控制台日志显示没有错误,如果文件不存在,则正在创建文件,并且正在正确读取.然而事实并非如此.当我在我的设备上构建并运行时,不会保存数据.此外,在指定的位置也不会创建任何文件.

我控制台记录了它试图使用的路径,这就是它:
文件:///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

我究竟做错了什么?

我假设您在Android中遇到此问题,因为我也面临同样的问题.根据我的理解和谷歌搜索,在android(至少在android 5)你不能写在应用程序数据目录,除非手机是root.所以你可能不得不使用externalRootDirectory.

例如:window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,successCallback,errorCallback);

希望能帮助到你.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...