How to allow access to camera in Android WebView

Viewed 561

I wanted to load a website in Android WebView which uses a device camera. But on Android WebView I'm not able to access the device camera

When I run: navigator.mediaDevices.getUserMedia({ video: true }) in JavaScript It does not ask for permission for camera and gives following error:

enter image description here

I have added the following permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Thank You!!

1 Answers

You will still need to request the user grants permission for your app to use the camera. I don't think you can request permission for this within your WebView (you can implement a JavaScript interface though to trigger it via your web app), so you will need to do it natively in Java/Kotlin on your app (maybe you want to do this on boot)

You can request permission by doing something like this

    requestPermissions(this, arrayOf(Manifest.permission.CAMERA), REQUEST_CODE)

You can see more info on requesting permissions to use hardware on Android here

Related