Cordova 文件插件的目录错误 [英] Cordova File Plugin Has Wrong Directory

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

问题描述

所以我在 angularjs 1 中构建了一个 cordova/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.

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

这是我用来打开文件的代码:

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

这是我保存文件的代码:

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

我已经使用本教程实现了这一目标:Cordova 文件插件教程

I have used this tutorial to get this far: Cordova File Plugin Tutorial

我做错了什么?

推荐答案

我假设您在 Android 中遇到了这个问题,因为我也遇到了同样的问题.根据我的理解和谷歌搜索,在 android 中(至少在 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);

希望有帮助.

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

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