Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the camera from within a Webview?

Tags:

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.

Everything else on the html page is shown.

Here are the permission I am putting in the 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" /> 

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

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

like image 531
McLan Avatar asked Nov 17 '16 15:11

McLan


People also ask

Can we access camera in WebView in android?

Embedding in an Android App requires the use of the WebView class. To allow EnableX to access the camera, follow the steps given below: Override WebChromeClient.

How do I open a link in WebView?

Within the shouldOverrideUrlLoading() method simply get the URL from the request and pass into the Intent. See the full example.


1 Answers

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

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());       }    }   }); 
like image 62
meuser07 Avatar answered Oct 06 '22 15:10

meuser07