Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need permission to access camera in Android web-view?

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?

like image 889
Pronab Roy Avatar asked Aug 23 '17 10:08

Pronab Roy


People also ask

How do you access the camera from Webview flutter?

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.

How do I request permission in Android 11?

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.


3 Answers

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

like image 101
AskNilesh Avatar answered Sep 17 '22 17:09

AskNilesh


I think that the best way here is to use WebView API to grant and deny permissions.

  1. Create class that extends WebChromeClient;
  2. Override onPermissionRequest()
  3. Request asked permission inside your app, or handle request in another way.
  4. Also you probably will need to override onPermissionRequestCanceled()
  5. Set an instance of your client to WebView webView.setWebChromeClient(chromeClient);
like image 32
Ufkoku Avatar answered Sep 17 '22 17:09

Ufkoku


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");
like image 20
SAIR Avatar answered Sep 17 '22 17:09

SAIR