getUserMedia fails in an Android WebView with no permission prompt
NotAllowedError from getUserMedia inside a WebViewShort answer
A WebView never shows the camera prompt by itself. It hands the request to your app through WebChromeClient.onPermissionRequest, and if you did not override it the request is denied. Override it, grant only the resources you want, and make sure the app itself holds the Android camera and microphone permissions.
Fix it fast
1Find out which cause you have
Check whether your WebView has a WebChromeClient that overrides onPermissionRequest. If it does not, that alone explains the immediate denial.
grep -rn "onPermissionRequest\|WebChromeClient" app/src/main/2Match what you see, then jump to its fix
- 1
The page's getUserMedia fails at once, and onPermissionRequest never shows up in your codeonPermissionRequest is not overriddenFix - 2
onPermissionRequest runs and grants, yet no stream arrivesThe app itself does not hold the Android camera or microphone permissionFix - 3
The callback runs but the page still sees a denial or an errorgrant or deny is called off the UI thread, or with the wrong resource listFix
- 1
What does the error look like?
NotAllowedError from getUserMedia inside a WebViewno permission dialog appears when the page calls getUserMedia
Cause 1 of 3
onPermissionRequest is not overridden
1Check
Search the project for a WebChromeClient subclass. The documented default is that, when the method is not overridden, the permission is denied.
grep -rn "webChromeClient\|WebChromeClient" app/src/main/2Fix
Set a WebChromeClient that grants only the capture resources you intend to allow, for your own origin.
webView.webChromeClient = object : WebChromeClient() {
override fun onPermissionRequest(request: PermissionRequest) {
runOnUiThread {
if (request.origin.host == "app.example.com") {
request.grant(arrayOf(
PermissionRequest.RESOURCE_VIDEO_CAPTURE,
PermissionRequest.RESOURCE_AUDIO_CAPTURE,
))
} else {
request.deny()
}
}
}
}3Verify
Load the page and call getUserMedia from the page's console or script: the request now reaches your callback and resolves.
navigator.mediaDevices.getUserMedia({ video: true }).then(s => console.log('ok', s.active));Confirmed by developer.android.com
Cause 2 of 3
The app itself does not hold the Android camera or microphone permission
1Check
Confirm the app declares the permissions in the manifest and that the user was asked at runtime. Android requires a runtime request for sensitive permissions on API 23 and later.
adb shell dumpsys package com.example.myapp | grep -A8 "runtime permissions"2Fix
Declare CAMERA and RECORD_AUDIO in the manifest, then request them with the RequestMultiplePermissions contract before you grant the WebView request.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
val launcher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { result -> /* grant or deny the saved PermissionRequest here */ }
launcher.launch(arrayOf(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO))3Verify
The dumpsys output now lists the permissions as granted=true.
adb shell dumpsys package com.example.myapp | grep -E "CAMERA|RECORD_AUDIO"Confirmed by developer.android.com
Cause 3 of 3
grant or deny is called off the UI thread, or with the wrong resource list
1Check
Look at where grant is called. The documentation says grant or deny must be called on the UI thread, and the resources passed must equal or be a subset of the requested ones.
grep -rn "request.grant\|\.grant(" app/src/main/2Fix
Wrap the call in runOnUiThread and pass explicit resource constants, never request.resources.
runOnUiThread {
request.grant(arrayOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
}3Verify
Request only video from the page: the grant completes and the stream starts.
navigator.mediaDevices.getUserMedia({ video: true }).then(s => console.log('ok', s.active));Confirmed by developer.android.com
How do I stop it coming back?
- Never write request.grant(request.resources): new resource types can appear in later WebView versions.
- Compare request.origin with your own domain before granting.
- Keep the runtime-permission request and the WebView grant in one code path, so one cannot run without the other.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Back end
navigator.mediaDevices is undefined, so getUserMedia cannot be called
navigator.mediaDevices is undefined
Browsers only expose navigator.mediaDevices on secure pages: HTTPS, localhost or file://.
3 causes, each with a fixMobile apps
Android build fails because a product flavor has no flavor dimension
The flavor 'flavor_name' is not assigned to a flavor dimension.
Android Gradle plugin refuses to build when a product flavor is not assigned to a flavor dimension.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
Android build fails because two libraries ship the same META-INF file
More than one file was found with OS independent path
Two dependencies put a file at the same path inside the APK and the plugin will not guess which one wins.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fix