Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access camera functionality in Android webview?

I'm building a Native-Android/WebView-App thats code is hosted on a remote server (in HTML and Javascript).

The Web App has a function that launches the phone's camera via a HTML form, input request:

<form action="#" method="post" enctype="multipart/form-data" >
<input name="image" type="file" accept="image/*" capture/>
</form>

This feature works perfectly well in the standard Android web browser but not in the Native App WebView browser.

I have enabled the following permissions in the Native app manifest but still can't get the camera to launch:

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

Failing getting this feature to work, I would accept being able to access the camera roll instead so that a user could simply select a photo they have already taken but I would rather launch the camera the way it does in the standard web browser.

Can anyone help please?

like image 203
CaptainJ Avatar asked Oct 30 '25 17:10

CaptainJ


1 Answers

While accessing Camera from Webview, it asks for permissions and we can grant them by overriding the onPermissionRequest. Also make sure to take care of runtime permissions.

webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onPermissionRequest(PermissionRequest request) {
            request.grant(request.getResources());
        }
    });

Also, make sure this property is disabled

webSettings.setMediaPlaybackRequiresUserGesture(false);
like image 176
VickyAbishek Avatar answered Nov 01 '25 07:11

VickyAbishek