【发布时间】:2021-04-04 22:47:28
【问题描述】:
我正在构建一个允许用户拍照的应用。我正在使用 Cordova - JS/CSS/HTML - 来编写脚本。 Cordova 相机插件在我的 Android Studio 中的 Android 模拟器上完美运行,但我无法让它在我的设备上运行。这是我正在使用的:
IRL 手机:三星 Galaxy S9+
科尔多瓦版本:10.0.0
Cordova 相机插件版本:5.0.1
PhoneGap cli-6.5.0(iOS 4.3.1 / Android 6.1.2 / Windows 4.4.3)
我不确定这是否与构建中的不一致有关(我听说 Cordova 版本可能会对某些环境中的插件产生影响),或者我的代码是否对真正的 Android 设备不友好。
这是插件代码:
let app = {
init: function(){
document.getElementById('btn').addEventListener('click', app.takephoto);
},
takephoto: function(){
let opts = {
quality: 80,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
mediaType: Camera.MediaType.PICTURE,
encodingType: Camera.EncodingType.JPEG,
cameraDirection: Camera.Direction.BACK,
targetWidth: 300,
targetHeight: 400
};
navigator.camera.getPicture(app.ftw, app.wtf, opts);
},
ftw: function(imgURI){
document.getElementById('msg').textContent = imgURI;
document.getElementById('photo').src = imgURI;
},
wtf: function(msg){
document.getElementById('msg').textContent = msg;
}
};
document.addEventListener('deviceready', app.init);
这是在单独文件夹中的文件 camera.js 中运行应用页面上脚本的 HTML:
<div class="page">
<p class="code"><img src="img/logo.png" alt="image" id="photo" /></p>
<p class="node"><button id="btn">Take Picture</button></p>
<p id="msg"></p>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script src="js/camera.js"></script>
这是我的头标签中的信息:
<meta http-equiv="Content-Security-Policy"
content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval';
style-src 'self' 'unsafe-inline';
media-src *;
script-src 'self' 'unsafe-inline';
img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>4-Point Inspection</title>
也许这与 XML 或构建文件有关?这是我的第一个 Cordova 应用程序,所以我对此有点陌生。任何想法为什么它可以在模拟器上而不是在我的 Galaxy S9+ 上运行?
编辑:我已经添加了我的 config.xml(在 \platforms\android\app\src\main\res\xml 中找到):
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<feature name="Whitelist">
<param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
<param name="onload" value="true" />
</feature>
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<allow-intent href="market:*" />
<preference name="loglevel" value="DEBUG" />
</widget>
这里是 AndroidManifest(在 \platforms\android\app\src\main 中找到):
<?xml version='1.0' encoding='utf-8'?>
【问题讨论】:
-
请分享
config.xml和AndroidManifest。 -
除了提供这两个文件.. 你的模拟器和真实设备上的 Android 版本是多少?
-
我继续添加了 config.xml 和 AndroidManifest 从 android 应用程序的“主”文件夹中。我将代码添加到原始帖子的末尾。我希望那些是正确的(我注意到所有文件夹中有一些 config.xml)。此外,我使用的是 Android 版本 10,我的模拟器是 Pixel XL,API 30。
标签: javascript android cordova android-emulator phonegap