Sunday, 4 September 2022

How to take a image shot from camera in webview android app

I have a simple web app, running in WebView, so complete android application is just one WebView and URL set to my page. In web app, I have a button that user clicks to take a shot from camera prior to continue to another part of app. All of this works just fine when in regular phone browser. However in WebView is not working.

In HTML page there is file input field, which is hidden:

<input type="file" name="fileToUpload" id="fileToUpload" onchange="receiveAShot();" accept="image/*" capture="camera" style="display:none;" />

and button which is performing action:

$(document).on("click", "#buttonId", function () {
    $("#fileToUpload").click();
})

and this works like a charm in phone web browsers (checked Chrome and Firefox).

I have set permissions to android app to

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

and for WebView in Main activity:

WebView webView=findViewById(R.id.webview);

WebSettings settings = webView.getSettings();
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://some.url.com");

I'm sure that javascript works regularly, since I was able to login (not classic form, just ajax request). But when I click button to start capture image, nothing happens at all. Any idea?



from How to take a image shot from camera in webview android app

No comments:

Post a Comment