我希望在Android WebView中使用QR码扫描器。 PWA在Chrome浏览器上运行良好。我已经在应用程序->应用程序名称->权限下设置了相机权限。测试了在安卓9的华为手机上,最低sdk版本是26,目标sdk版本是28。问题在于权限似乎没有被授予。此外,权限请求会多次重复。
如果我直接调用getUserMedia,问题仍然存在。
在MainActivity.kt中。
Android 代码
清单文件 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
根据来自之前的问题的答案, 我在 WebViewHelper 类中有以下代码:
lateinit var webkitPermissionRequest : PermissionRequest
...
webView.webChromeClient = object : WebChromeClient() {
override fun onPermissionRequest(request: PermissionRequest) {
webkitPermissionRequest = request
val requestedResources = request.resources
for (r in requestedResources) {
if (r == PermissionRequest.RESOURCE_VIDEO_CAPTURE) {
// In this sample, we only accept video capture request.
val alertDialogBuilder = AlertDialog.Builder(activity)
.setTitle("Allow Permission to camera")
.setPositiveButton("Allow") { dialog, which ->
dialog.dismiss()
webkitPermissionRequest.grant(arrayOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
Log.d(TAG, "Granted")
}
.setNegativeButton("Deny") { dialog, which ->
dialog.dismiss()
webkitPermissionRequest.deny()
Log.d(TAG, "Denied")
}
val alertDialog = alertDialogBuilder.create()
alertDialog.show()
break
}
}
}
...
}
日志: 权限被授予后,会再次请求(多次)
D/WebViewHelper: Granted
V/InputMethodManager: Reporting focus gain, without startInput
I/PermissionManager: camera remind result:true
I/CameraManager: open camera: 1, package name: "myApp"
I/BackgroundPermManager: pkgName: "myApp", pid: 31365 ,uidOf3RdApk: 10197 ,permType: 0 ,permCfg: 1
I/HwCameraUtil: notifySurfaceFlingerCameraStatus : isFront = true , isOpend = true
I/HwCameraUtil: notifySurfaceFlingerFrontCameraStatus 8011 transact success!
E/cr_VideoCapture: CameraDevice.StateCallback onOpened
I/WebViewHelper: onPermissionRequest
onPermissionRequest
这似乎是问题所在
I/GRALLOC: LockFlexLayout: baseFormat: 11, yStride: 640, ySize: 307200, uOffset: 307200, uStride: 640
E/ion: ioctl c0044901 failed with code -1: Invalid argument
I/chromium: "Unhandled rejection", source: "PWA
"Uncaught (in promise) NotAllowedError: play() can only be initiated by a user gesture.", source: "PWA"
最后,这个错误一直重复出现。
I/GRALLOC: LockFlexLayout: baseFormat: 11, yStride: 640, ySize: 307200, uOffset: 307200, uStride: 640
JavaScript 代码
JS 侧面的代码在 WebView 中打开之前运行得非常好。
app.ports.scanQR.subscribe(() => {
// Delay until page loaded
setTimeout(function(){
const video = document.getElementById('media-video');
function returnResult(result) {
app.ports.onGotQR.send(result);
scanner.destroy();
}
const scanner = new QrScanner(video, result => returnResult(result));
scanner.start();
}, 50);
});
}
如果我直接调用getUserMedia,问题仍然存在。
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
})
.catch(function (err0r) {
console.log("Something went wrong!");
});
}
获取相机应用权限:
在WebviewHelper.kt中
webView.webChromeClient = object : WebChromeClient() {
override fun onPermissionRequest(request: PermissionRequest) {
Log.i(TAG, "onPermissionRequest")
// grants permission for app. video not showing
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED
) {
Log.i(TAG, "Request Permission")
requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), 1010)
} else {
Log.i(TAG, "Permission already granted")
}
...
}
在MainActivity.kt中。
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
cameraRequestCode -> {
Log.d("MainActivity", "onRequestPermissionsResult: Camera Request")
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Log.d("MainActivity", "Camera Request: Permission granted")
// permission was granted, yay!
} else {
// permission denied, boo!
Log.d("MainActivity", "Camera Request: Permission denied")
}
return
}
...
当相机应用权限被授予时,以下日志结果是预期的:
D/MainActivity: onRequestPermissionsResult: Camera Request
Camera Request: Permission granted
alertDialog
进行此操作了吗? 如果不是,我该如何获取用户的手势? - Noah Studach