如何从 Webview 中访问摄像头?

How to access the camera from inside a Webview?

我们连接到 https。如果我在 Android 设备上使用 chrome 浏览器,我们可以在启用摄像头和麦克风的情况下进行连接。也可以打开和关闭它们。但是,如果我们尝试使用 webview 进行相同的操作,我们甚至不会提示网站授权访问并在尝试打开它们时出现“无法访问 camera/mic”错误。

清单

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

网页视图Activity

class WebView : AppCompatActivity() {

    val permission = arrayOf(Manifest.permission.CAMERA,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.MODIFY_AUDIO_SETTINGS)
    val requestCode = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_view)

        WebViewSetup()


        if (!isPermissionGranted()) {
            askPermissions()


        }


    }


    private fun askPermissions() {
        ActivityCompat.requestPermissions(this, permission, requestCode)
    }

    private fun isPermissionGranted(): Boolean {
        permission.forEach {
            if (ActivityCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED)
                return false
        }

        return true
    }


    @SuppressLint("SetJavaScriptEnabled")
    private fun WebViewSetup() {


        val url = intent.extras!!.getString("url")
        webview.webChromeClient = WebChromeClient()
        webview.apply {
            if (url != null) {
                loadUrl(url)
            }

            Log.d("callBtn", "Url ::  $url!!!! ")
            settings.javaScriptEnabled = true
            settings.javaScriptCanOpenWindowsAutomatically = true
            settings.domStorageEnabled = true
            settings.javaScriptCanOpenWindowsAutomatically = true
            settings.allowContentAccess = true
            settings.safeBrowsingEnabled = true
            settings.mediaPlaybackRequiresUserGesture = false
        }
    }
}

这是答案

class WebViewActivity : AppCompatActivity() {

private val permission = arrayOf(Manifest.permission.CAMERA,
    Manifest.permission.RECORD_AUDIO,
    Manifest.permission.MODIFY_AUDIO_SETTINGS)
private val requestCode = 1

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_web_view)
    val action: String? = intent?.action
    val data: Uri? = intent?.data

    WebViewSetup()
    if (!isPermissionGranted()) {

        askPermissions()

    }

    webview.webChromeClient = object : WebChromeClient() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        override fun onPermissionRequest(request: PermissionRequest) {
            request.grant(request.resources)
        }
    }

}


private fun askPermissions() {
    ActivityCompat.requestPermissions(this, permission, requestCode)
}

private fun isPermissionGranted(): Boolean {
    permission.forEach {
        if (ActivityCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED)
            return false
    }

    return true
}

@SuppressLint("SetJavaScriptEnabled")
private fun WebViewSetup() {

    val url = intent.extras!!.getString("url")
    webview.webChromeClient = WebChromeClient()


    webview.apply {
        if (url != null) {
            loadUrl(url)
        }

        Log.d("callBtn", "Url ::  $url!!!! ")
        settings.javaScriptEnabled = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true
        settings.allowContentAccess = true
        settings.safeBrowsingEnabled = true
        settings.mediaPlaybackRequiresUserGesture = false


    }
}

}