簡體   English   中英

Cordova 相機插件適用於模擬器,而不適用於 Android 設備

[英]Cordova camera plugin works on emulator and not on Android device

我正在構建一個允許用戶拍照的應用程序。 我正在使用 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);

這是 HTML 從單獨文件夾中的文件 camera.js 運行應用程序頁面上的腳本:

        <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'?>

在啟動相機之前檢查用戶是否允許相機的應用程序權限,例如您在安裝應用程序后第一次打開相機時的日常社交應用程序請求。 在此之前轉到應用程序管理器並啟用相機權限並檢查相機是否打開。

您可以使用https://www.npmjs.com/package/cordova-plugin-android-permissions獲取應用程序權限,但僅適用於 android 不適用於 Z9E304D4E8DF1B74CFA009913198428

嘗試更新您的小部件標簽,因為您的 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">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM