要访问Android Webview中的相机功能? [英] Access camera functionality in Android webview?
问题描述
我正在构建一个Native-Android/WebView-App,该代码托管在远程服务器上(使用HTML和Javascript).
I'm building a Native-Android/WebView-App thats code is hosted on a remote server (in HTML and Javascript).
Web应用程序具有通过HTML表单,输入请求启动手机摄像头的功能:
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>
此功能在标准的Android Web浏览器中可以很好地工作,但在Native App WebView浏览器中则不能很好地工作.
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" />
无法使用此功能,我会接受能够访问照相机胶卷,以便用户可以简单地选择他们已经拍摄的照片,但是我宁愿像在标准Web浏览器中那样启动照相机.
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?
推荐答案
从Webview访问Camera时,它会请求权限,我们可以通过覆盖onPermissionRequest来授予它们.另外,请务必注意运行时权限.
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);
这篇关于要访问Android Webview中的相机功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!