[英]cordova.file is undefined in ionic project for android
I'm trying to build an ionic app
for android
. 我正在尝试为
android
构建一个ionic app
。 I need to read a file from the sdcard for which I'm using ngcordova plugin
. 我需要从sdcard读取一个文件,我正在使用
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
. 我已经要求
ngCordova
角模块在app.js
我注射ngcordova
的前cordova.js
在我index.html
。 Installed it using 使用安装它
cordova plugin add cordova-plugin-file
cordova插件添加cordova-plugin-file
I have injected $cordovaFile
in my controller. 我在我的控制器中注入了
$cordovaFile
。
Whenever I try to read a file using the below code, I get the error, cannot read property externalDataDirectory of undefined
每当我尝试使用下面的代码读取文件时,我都会收到错误,
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 我也试图打印cordova,但它不包含文件对象
$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
我也已经完成
build cordova android
删除并添加了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
. 我正在设备上测试这个并在每次测试时构建一个新的apk,而不是在
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. 使用任何额外的插件配置更新了config.xml文件,这可能解决了您所面临的问题。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.