如何从 Webview 中访问摄像头? [英] How to access the camera from within a Webview?

查看:117
本文介绍了如何从 Webview 中访问摄像头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 android 应用程序中,我试图在 WebView 上加载一个网页(必须访问相机).在我的笔记本电脑上,当我加载网页时,我可以访问相机.

In my android app, I am trying to load a webpage (that must access the camera) on WebView. On my laptop, when I load the webpage, I could access the camera.

显示 html 页面上的所有其他内容.

Everything else on the html page is shown.

这是我在 Manifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

我将 SDK 设置如下:

I am setting the SDK as follow:

<uses-sdk
    android:minSdkVersion="18"
    android:targetSdkVersion="21" />

这是我的网络视图设置:

Here is my webview setting:

private void setMyWebviewSettings(WebSettings MyWebviewSettings) {
    MyWebviewSettings.setAllowFileAccessFromFileURLs(true);
    MyWebviewSettings.setAllowUniversalAccessFromFileURLs(true);
    MyWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    MyWebviewSettings.setJavaScriptEnabled(true);
    MyWebviewSettings.setDomStorageEnabled(true);
    MyWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    MyWebviewSettings.setBuiltInZoomControls(true);
    MyWebviewSettings.setAllowFileAccess(true);
    MyWebviewSettings.setSupportZoom(true);
}

如果我可以直接从我的应用程序访问相机(使用普通活动),为什么我不能从 WebView 中打开它?!

If I could access the camera from my app directly (using a normal activity), why can't I open it from within the WebView?!

推荐答案

我正在尝试同样的事情.下面的代码对我有用.

I was trying same thing. Below code worked to me.

首先在清单文件中,我们必须使用使用权限标签添加相机硬件权限为 true.

First in manifest file we have to add camera hardware permission true using uses permission tag.

然后需要接受相机权限才能在您的活动中使用以下行.

Then need to accept camera permission to use using below lines in your activity.

webview.setWebChromeClient(new WebChromeClient() {

   @Override
   public void onPermissionRequest(final PermissionRequest request) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         request.grant(request.getResources());
      }
   }

 });

这篇关于如何从 Webview 中访问摄像头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆