如何解决Cordova 相机插件适用于模拟器而不适用于 Android 设备
我正在构建一个允许用户拍照的应用。我正在使用 Cordova - JS/CSS/HTML - 编写脚本。 Cordova 相机插件在 Android Studio 中的 Android 模拟器上完美运行,但我无法在我的设备上使用它。这是我正在使用的:
IRL 手机:三星 Galaxy S9+
Cordova 版本: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>
这是我的 head 标签中的信息:
<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'?>
解决方法
在开始相机检查之前,用户允许相机的应用内权限,例如您在安装应用后第一次打开相机时的日常社交应用请求。在此之前转到应用管理器并启用相机权限并检查相机是否打开。
您可以使用 https://www.npmjs.com/package/cordova-plugin-android-permissions 获取应用内权限,但仅适用于 android,不适用于 ios
,尝试更新您的小部件标签,因为您的 android 命名空间缺少 xmlns:android="http://schemas.android.com/apk/res/android
<widget id="com.your.app.id" version="1.0.0"
xmlns="http://www.w3.org/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。