Virsexpon
Virsexpon

Reputation: 1

Open camera from webview

How can I open the camera from the webview when I use webChromeClient?

In HTML I use: <input id="input-1" class="input-upload" type="file" accept="jpg/*|png/*|gif/*|image/*" style="display:none;"/>

and in Android I use:

 webSettings.setJavaScriptEnabled(true);
 mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
 mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

 mWebView.setWebChromeClient(new WebChromeClient(){
     @Override
     public void onPermissionRequest(final PermissionRequest request) {
         request.grant(request.getResources());
     }

with these permissions:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="true" />

This is working in the Chrome browser under Android. I have no idea why it's not working in a webview on the same phone.

Upvotes: 0

Views: 1995

Answers (2)

Avinash kumawat
Avinash kumawat

Reputation: 75

On Android app Use callbacks.. 1.) Create a call back class 'ExampleCallback'. Add annotation on the dataResponse

   class ExampleCallback{

     @JavascriptInterface
        public void dataResponse(String res) {
//Put your code to open camera
    }
    }

2.) add this callback to your webview

webView.addJavascriptInterface(new ExampleCall(this), "Android");

On your html page create a java script function with name Android and call it when you want to open the camera. java script would be like.

<script>

    function showAndroidToast(res) {

        Android.dataResponse(res);
    }

</script>

Upvotes: 0

Related Questions