I am developing a Web App with the help of Webview
in android studio but having some issue I need to have the access open the camera how can I do that I have given the following permission in AndroidManifest.xml
<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" />
what should I need to do more to open the camera?
In order to be able to use camera, for example, for taking images through <input type="file" accept="image/*" capture> HTML tag, you need to ask camera permission. If you need to capture video and audio, you need to ask also microphone permission.
Request permissions If the permission is not granted, we need to ask for permission to access certain features like camera, storage, etc. To ask permission, we need to use ActivityCompat. requestPermission() method with the permission name and the request code.
if your app targeting Android 6.0 and above than add runtime permission
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app
add runtime permission using below code for camera
String permission = Manifest.permission.CAMERA;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
and than handle result like this
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();
// perform your action here
} else {
Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
read about runtime permission
I think that the best way here is to use WebView API to grant and deny permissions.
WebChromeClient
;onPermissionRequest()
onPermissionRequestCanceled()
WebView
webView.setWebChromeClient(chromeClient);
If you are developing a Web App using the webView then you should learn about JavascriptInterface
provided by Android. Go through following links
Javascript interface
Android webView
All you need to do is setup javascript call back.call that method from your web page, handle the javascript call back in the Activity/fragment and from there you can open the camera.
You should also check the answer of @Nilesh Rathod for the run time permissions.
here are the code snippets that will help you
private class WebAppInterface {
public WebAppInterface(Context context) {
}
@JavascriptInterface
public void openCamera(String title) {
//check permissions and open camera intent;
}
}
mWebView.addJavascriptInterface(new WebAppInterface(this), "PLATFORM_ID");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With