Access the camera of the mobile phone in a Webview in Android Studio

Asked

Viewed 2,046 times

5

I am creating a QR code reader on a website and now I need to use this site in a mobile version, for this I am creating a Webview that is opening the site.

In any browser the QR Code reader works, on desktop or mobile. However when I use the webview it does not give permission to use the Android camera.

I’ve entered all permissions and checks I found on the internet but nothing works on any version of Android (I’m using 6.0)

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I saw an option to insert a Javascript to call the camera in Webview but I could not find how it can be done.

If necessary send the APP code.

  • Do you use Cordova?

1 answer

1

Try assigning permissions to the class as well, and load your HTML with Loadurl:

WebView wView = (WebView) findViewById(R.id.seuWebView);

    wView.getSettings().setJavaScriptEnabled(true);
    wView.getSettings().setAllowFileAccessFromFileURLs(true);
    wView.getSettings().setAllowUniversalAccessFromFileURLs(true);

    wView.setWebViewClient(new WebViewClient());
    wView.setWebChromeClient(new WebChromeClient() {
        // Grant permissions for cam
        @Override
        public void onPermissionRequest(final PermissionRequest request) {
            Log.d("Log1", "onPermissionRequest");
            runOnUiThread(new Runnable() {
                @TargetApi(Build.VERSION_CODES.M)
                @Override
                public void run() {
                    Log.d("Log2", request.getOrigin().toString());
                    if(request.getOrigin().toString().equals("file:///")) {
                        Log.d("Log3", "GRANTED");
                        request.grant(request.getResources());
                    } else {
                        Log.d("Log4", "DENIED");
                        request.deny();
                    }
                }
            });
        }


    });

    wView.loadUrl(SEU_HTML);

And as for JS, use getMédia to set the video to an HTML tag, in this case you create a tag <video> in your HTML and do the following:

var constraints = { video: { width: 800, height: 800 } };

    navigator.mediaDevices.getUserMedia(constraints)
            .then(function(mediaStream) {
        var video = document.querySelector('video');
        video.srcObject = mediaStream;
        video.onloadedmetadata = function(e) {
            video.play();
        };
    }).catch(function(err) { console.log(err.name + ": " + err.message); });

Try these two things there, and put this permission on the manifest, just to be sure:

<uses-permission android:name="android.permission.INTERNET" />

Thanks, bro, it’s Noiz :D.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.