Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angularjs - Cordova File Plugin Has Wrong Directory

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.

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

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

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...