Search code examples
angularjscordovacordova-plugins

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

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

What am I doing wrong?


Solution

  • 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.