delight-im / Android-AdvancedWebView

Enhanced WebView component for Android that works as intended out of the box
MIT License
2.39k stars 574 forks source link

Camera access permission #20

Closed moirrer closed 9 years ago

moirrer commented 9 years ago

Is there a way to give my android app camera access permission? Like mWebView.setGeolocationEnabled(true) for location permission, is there anything like it to allow the camera access?

ocram commented 9 years ago

Thanks for your question!

It's actually not as easy as with the geolocation, unfortunately. But there are still ways to do it:

First, there's HTML5 media capture. Adding this to your WebView is possible, but requires some work. You can read more about that here:

Apart from that, there's a newer API called navigator.getUserMedia() in JavaScript. But this API is not supported before Android 5.0 -- and even then, it's only supported with the "webkit" prefix. You can read more about that API here:

Hope that helps!

moirrer commented 9 years ago

Manage to make it work! Once you have your camera capture working in webbrowser, to grant the camera permission:

public void iniciaWebView(){
    mWebView = (AdvancedWebView) findViewById(R.id.webview);
    mWebView.setListener(this, this);
    mWebView.setGeolocationEnabled(true);
    mWebView.addHttpHeader("X-Requested-With", appNomeLogs);

    mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onPermissionRequest(final PermissionRequest request) {
            Log.i(appNomeLogs, "|> onPermissionRequest");

            MainActivity.this.runOnUiThread(new Runnable(){
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void run() {
                    Log.i(appNomeLogs, "|> onPermissionRequest run");
                    request.grant(request.getResources());
                }// run
            });// MainActivity

        }// onPermissionRequest
    });// setWebChromeClient

    mWebView.loadUrl(url);
}// iniciaWebView

Only tested in android 5 so far.

moirrer commented 9 years ago

obs: not forgeting the manifest

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
ocram commented 9 years ago

Thanks for sharing! Great that it worked for you :)

But getting this to work on Android 4.0+ as well and saving the captured image on the file system probably requires some more work, right?

RoelRoel commented 7 years ago

@moirrer Looking at your code it looks like this will auto-accept all permissions requested. When you allow external websites in your webview you don't have control off, I think you don't want this without the users permission.

ocram commented 7 years ago

@RoelRoel That is correct. You should only ever do this if your own website is the only site that will be displayed in the WebView. But even then, you have to pay attention because external links might lead to external sites again.

jcrooke commented 7 years ago

For what it's worth, I ended up using parts of https://github.com/mgks/Os-FileUp to get it working.