webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
// 处理拍照功能
return true;
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setMediaPlaybackRequiresUserGesture(false);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setSupportMultipleWindows(true);
webSettings.setSupportZoom(false);
由于在Android端WebView中访问本地网络摄像头需要获得用户的授权,因此,需要在将要访问本地摄像头之前,提供询问用户是否授权的接口,以获取用户的同意。示例如下所示:
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
} else {
// 已经获取了摄像头访问授权
openCamera();
}
} else {
// 低于Android 6.0,无需处理运行时权限
openCamera();
}
}