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
I build the app on a different machine than the one i develop and i use git to sync them. Plugins are in .gitignore hence I was not able to access them. ionic state reset helped me fix this issue
Source: javascript - cordova.file is undefined in ionic project for android - Stack Overflow