【问题标题】:navigator.mediaDevices.getUserMedia is not working with Android 7 and Ionic 1navigator.mediaDevices.getUserMedia 不适用于 Android 7 和 Ionic 1
【发布时间】:2020-04-26 10:16:22
【问题描述】:

我正在使用 ionic 1 。 我的应用正在使用 Cordova Plugin Camera Preview (9.0.0 (cordova-lib@9.0.1)) 实时显示相机

我正在桌面上使用此命令预览 apk:

ionic cordova run android

我的代码非常好适用于使用android 5的homtom手机

但在我的Android 7 Doggee手机上,显示的是一个大三角形,我什至无法点击它拍照。

这是我的代码:

var videoElement = document.getElementById("video");
var videoSrc = undefined;

navigator.mediaDevices.enumerateDevices()
  .then(getDevices).then(getStream).catch(handleError);

function getDevices(deviceInfos) {
  for (var i = 0; i !== deviceInfos.length; ++i) {
    var deviceInfo = deviceInfos[i];
    if (deviceInfo.kind === 'videoinput') {
      videoSrc = deviceInfo.deviceId;
      break;
    }
  }
}

function getStream() {    
  navigator.mediaDevices.getUserMedia({
    video: {
      deviceId: {
        exact: videoSrc
      }
    }
  }).
  then(gotStream).catch(handleError);
}

function gotStream(stream) {
  videoElement.srcObject = stream;
}

function handleError(error) {
  console.log('Error: ', error);
}

还有我的 myproject/platforms/android/app/src/amin/AndroidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="io.ionic.starter" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.cordovaplugincamerapreview.CameraActivity" android:screenOrientation="portrait" android:theme="@style/CameraPreviewTheme" />
    </application>
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature android:name="android.hardware.camera2.full" />
    <uses-feature android:name="android.hardware.camera2.autofocus" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.webkit.PermissionRequest" />
    <uses-feature android:name="android.hardware.camera" android:required="true" />
</manifest>

然后在我的 \myproject\platforms\android\app\src\main\res\xml\config.xml 我只有这个相机功能:

 <feature name="CameraPreview">
        <param name="android-package" value="com.cordovaplugincamerapreview.CameraPreview" />
        <param name="onload" value="true" />
    </feature>

这是电话控制台中的错误

Error:  DOMException: Could not start source 

编辑:终于部分解决了: 我必须进入应用程序的设置 并允许它访问相机。我以为我在 清单文件,但它似乎没有为我授予它。

斯科特

【问题讨论】:

    标签: android cordova ionic-framework


    【解决方案1】:

    使用诊断插件检查并询问android权限解决了我的问题。

    checkpermission()
      {
        this.diagnostic.getPermissionAuthorizationStatus(this.diagnostic.permission.CAMERA)
        .then((status) => {
            switch(status){
                case this.diagnostic.permissionStatus.GRANTED:
                    console.log("Permission granted to use the camera");
                    break;
                case this.diagnostic.permissionStatus.NOT_REQUESTED:
                    console.log("Permission to use the camera has not been requested yet");
                    break;
                case this.diagnostic.permissionStatus.DENIED:
                    console.log("Permission denied to use the camera - ask again?");
                    break;
                case this.diagnostic.permissionStatus.DENIED_ALWAYS:
                    console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                    break;
            }
        })
        .catch((error) => {
            console.error("The following error occurred: "+error);
        });
    
    
    this.diagnostic.requestRuntimePermission(this.diagnostic.permission.CAMERA)
    .then((status) => {
        switch(status){
            case this.diagnostic.permissionStatus.GRANTED:
                console.log("Permission granted to use the camera");
                break;
            case this.diagnostic.permissionStatus.NOT_REQUESTED:
                console.log("Permission to use the camera has not been requested yet");
                break;
            case this.diagnostic.permissionStatus.DENIED:
                console.log("Permission denied to use the camera - ask again?");
                break;
            case this.diagnostic.permissionStatus.DENIED_ALWAYS:
                console.log("Permission permanently denied to use the camera - guess we won't be using it then!");
                break;
        }
    })
    .catch((error) => {
        console.error("The following error occurred: "+error);
    });
    }
    

    【讨论】:

    • 请用代码部分重新格式化您的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 2016-09-24
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2018-08-11
    相关资源
    最近更新 更多