Access camera functionality in Android webview?

流过昼夜 提交于 2020-08-09 07:13:28

问题


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?


回答1:


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);



回答2:


Use Javascript interface to launch camera.

Or

You can use cordova Framework.



来源:https://stackoverflow.com/questions/31461749/access-camera-functionality-in-android-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!