科尔多瓦文件插件有错误的目录 [英] Cordova File Plugin Has Wrong Directory

查看:127
本文介绍了科尔多瓦文件插件有错误的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我建设angularjs 1科尔多瓦/ PhoneGap的应用程序,我试图保存和在应用程序的私有目录/沙箱一个名为calendar.txt文件读取,不能。

So I am building a cordova/phonegap app in angularjs 1 and I'm trying to save and read from a file called calendar.txt in the app's private directory/sandbox and can't.

在调试时,我的控制台日志显示没有错误,如果它不存在正在创建的文件,被正确读取。但是,这种情况并非如此。当我建立我的设备上运行,数据不会被保存。也没有任何文件在指定的位置创建。

My console logs while debugging show that there are no errors and the file is being created if it doesn't exist, and is being read correctly. However that is not the case. When I build and run on my device, the data is not saved. Also no file is created in the location specified.

我登录控制台,它试图使用路径,这是它:
文件:///data/data/com.adobe.phonegap.app/files/calendar.txt

I console logged the path it was trying to use and this is it: file:///data/data/com.adobe.phonegap.app/files/calendar.txt

下面是code我用来打开文件:

Here is the code I am using to open the file:

$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);
            }
        });
    };

这里是code为我保存文件:

And here is the code for my save the file:

$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);
            }
        );
    };

我已经使用这个教程走这么远:科尔多瓦文件插件教程

我是什么做错了吗?

推荐答案

我假设你正面临着​​Android的这个问题,因为我也面临着同样的。按我的理解,谷歌搜索,在android系统(ATLEAST在android系统5)你不能在应用程序数据目录中,除非手机植根写。所以,你可能不得不使用externalRootDirectory来代替。

I assume that you are facing this issue in Android as i too faced the same. As per my understanding and googling, in android (atleast in android 5) you cant write in application data directory unless the phone is rooted. So you may have to use externalRootDirectory instead.

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

希望它帮助。

这篇关于科尔多瓦文件插件有错误的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆