Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

getUserMedia fails in an Android WebView with no permission prompt

ErrorNotAllowedError from getUserMedia inside a WebView

Short 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.

  • Applies to Android 14, Android 15, Kotlin, androidx.activity 1.2+
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The page's getUserMedia fails at once, and onPermissionRequest never shows up in your codeonPermissionRequest is not overriddenFix
    2. 2onPermissionRequest runs and grants, yet no stream arrivesThe app itself does not hold the Android camera or microphone permissionFix
    3. 3The 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

What does the error look like?

  • NotAllowedError from getUserMedia inside a WebView
  • no 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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.