简体   繁体   中英

cordova.file is undefined in ionic project for android

I'm trying to build an ionic app for android . I need to read a file from the sdcard for which I'm using ngcordova plugin . I have required ngCordova in the angular module in app.js I am injecting the ngcordova before the cordova.js in my index.html . Installed it using

cordova plugin add cordova-plugin-file

I have injected $cordovaFile in my controller.

Whenever I try to read a file using the below code, I get the error, cannot read property externalDataDirectory of undefined

      $ionicPlatform.ready(function() {
            console.log('cordova.file.externalDataDirectory: ' + cordova.file.externalDataDirectory);
            myFsRootDirectory1 = 'file:///storage/emulated/0/'; // path for tablet
            myFsRootDirectory2 = 'file:///storage/sdcard0/'; // path for phone
            fileTransferDir = cordova.file.externalDataDirectory;
            if (fileTransferDir.indexOf(myFsRootDirectory1) === 0) {
                fileDir = fileTransferDir.replace(myFsRootDirectory1, '');
            }
            if (fileTransferDir.indexOf(myFsRootDirectory2) === 0) {
                fileDir = fileTransferDir.replace(myFsRootDirectory2, '');
            }
            console.log('Android FILETRANSFERDIR: ' + fileTransferDir);
            console.log('Android FILEDIR: ' + fileDir);

            $cordovaFile.readAsText(fileDir + 'a.csv').then(function(result) {
                    console.log('readAsText: ', result);
                    alert(result)
                })
                .catch(function(err) {
                    alert(err)
                });
        })

I have tried to print the cordova as well, but it does not contain the file object

     $ionicPlatform.ready(function() {
        alert(angular.toJson(window.cordova))
    })

I have also done build cordova android removed and added the cordova plugin rm cordova-plugin-file cordova plugin add cordova-plugin-file

Here are the versions:

"ionic": "driftyco/ionic-bower#1.3.1",
"ngCordova": "^0.1.27-alpha"

I'm testing this on the device and building a new apk each time for testing, not in webview .

Please help me resolve this issue or if you could recommend me a different method to get around this issue, that would be great!

Thanks for the answers

Updated the config.xml file with any extra plugin configuration, maybe this solves the problem you are facing. Check the plugin documentation for more information.

<preference name="AndroidPersistentFileLocation" value="Compatibility" />             
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />

More information Plugin website

Update - try following

npm update -g
cordova -v
cordova platform list
cordova platform update ios (or whatever platforms)
cordova platform list
cordova plugin list
-- now either do a cordova plugin remove/add for each plugin to update
-- OR with ionic use ionic state reset and it will install all from package.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM