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
?!
This is certainly possible with getUserMedia through WebRTC.
Set up your WebView to allow permission and load your HTML with loadUrl():
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccessFromFileURLs(true);
myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient() {
// Grant permissions for cam
@Override
public void onPermissionRequest(final PermissionRequest request) {
Log.d(TAG, "onPermissionRequest");
MainActivity.this.runOnUiThread(new Runnable() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void run() {
Log.d(TAG, request.getOrigin().toString());
if(request.getOrigin().toString().equals("file:///")) {
Log.d(TAG, "GRANTED");
request.grant(request.getResources());
} else {
Log.d(TAG, "DENIED");
request.deny();
}
}
});
}
});
myWebView.loadUrl(LOCAL_FILE);
Then use getUserMedia through your JS file, assuming you have a <video>
tag in your HTML file:
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); }); // always check for errors at the end.
Lastly, ensure you have permissions set in your manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />