'Camera access in web view in android

I have below code added using other stacker over follow solution sites:

  mBinding.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                Log.d(TAG,"onPermissionRequest");
                
                mPermissionRequest = request;
                final String[] requestedResources = request.getResources();
                for (String r : requestedResources) {
                    if (r.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
                       requireActivity().runOnUiThread(() -> {

                           CustomDialogNew customDialog1 = CustomDialogNew.getInstance();

                           Runnable runnable = () -> {
                               customDialog1.dismiss();
                               mPermissionRequest.grant(new String[]{PermissionRequest.RESOURCE_VIDEO_CAPTURE});
                               Log.d(TAG, "Granted");
                               loadUrl(lastUrlLoaded);
                           };

                           customDialog1.init(0, getString(R.string.message),
                                   false, getString(R.string.ok), "", runnable,
                                   null);
                           customDialog1.show(getParentFragmentManager(), TAG);
                       });
                    }
                }
            }

            @Override
            public void onPermissionRequestCanceled(PermissionRequest request) {
                Log.d(TAG,"onPermissionRequestCanceled");
                //super.onPermissionRequestCanceled(request);
            }
        });

Above logic not working when I grant permission after that when I reload the url that time I see black screen for camera, camera not open as expected.

Any help is apperciated!



Solution 1:[1]

In Webview you need to have a method which triggers Android method and by which you can achieve you functionality. Search about JavascriptInterface Also make sure before using camera ask the relevant permissions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 drjansari