我无法从 Android 设备读取 www/config.xml 文件。我尝试了两种不同的方法。首先,我尝试使用另一个线程中推荐的 $http 服务。当那不起作用时,我尝试使用 Cordova 文件插件。我这样做的原因是尝试从应用程序中的 www/config.xml 文件中获取版本号。以下是两种方法:
1) $http.get()
url = 'file:///android_asset/www/config.xml'
$http.get(url)
.then ((data) ->
versionNumber = data.data.match(/<widget\s.*?\sversion=['"]([^'"]+)['"]/)
verNum = versionNumber[1]
), (err) ->
# {"data": null, "status": 0, .....}
alert JSON.stringify err
2) Cordova 文件插件
if ionic.Platform.isAndroid()
path = 'file:///android_asset/www/'
file = 'config.xml'
$cordovaFile.readAsText(path, file)
.then ((data) ->
alert JSON.stringify data
return
), (error) ->
# I alert { code: 5 } which is an ENCODING_ERR
alert JSON.stringify error
最佳答案
查看文件夹platforms/android,您可以看到文件'android_asset/www/config.xml'
不存在。
您可以尝试阅读文件 '/res/xml/config.xml'
或者您可以尝试不同的方法,例如创建 Cordova Hook .
Cordova Hooks:
Cordova Hooks represent special scripts which could be added by application and plugin developers or even by your own build system to customize cordova commands.
您可以调整 replace text hook from this article或者这个 build number hook基于同一个钩子(Hook)。
更新
要仅获取您的应用程序的版本号,您可以使用此插件:
Cordova AppVersion plugin
Cordova plugin to return the version number of the current app
Is available also with ngCordova
关于cordova - 从设备读取 ionic www/config.xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570152/