我正在构建一个允许用户拍照的应用程序。我正在使用 Cordova - JS/CSS/HTML - 来编写脚本。 Cordova 相机插件在我的 Android Studio 中的 Android 模拟器上完美运行,但我无法在我的设备上运行它。这是我正在处理的内容:
IRL Phone: Samsung Galaxy S9+
Cordova version: 10.0.0
Cordova camera plugin version: 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获得inApp权限但仅适用于android而不适用于ios
关于javascript - Cordova 相机插件适用于模拟器,不适用于 Android 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65472949/