Giacomo M
Giacomo M

Reputation: 4711

if I add <uses-permission android:name="android.permission.CAMERA" /> the camera in a web view DOES NOT work in Android 7.0

I have a webview with an input file, and I use this code to open the camera to pick the image:

// ...
private ValueCallback<Uri[]> mUMA;
// ...

webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
            // TARGET BLANK OPENED IN BROWSER
            WebView newWebView = new WebView(view.getContext());
            newWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(browserIntent);
                    return true;
                }
            });
            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(newWebView);
            resultMsg.sendToTarget();
            return true;
        }

        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }

        //For Android 5.0+
        public boolean onShowFileChooser(
                WebView webView, ValueCallback<Uri[]> filePathCallback,
                WebChromeClient.FileChooserParams fileChooserParams) {
            if (mUMA != null) {
                mUMA.onReceiveValue(null);
            }
            mUMA = filePathCallback;
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                File photoFile = null;
                try{
                    photoFile = createImageFile();
                    takePictureIntent.putExtra("PhotoPath", mCM);
                }catch(IOException ex) {
                    //Log.e(TAG, "Image file creation failed", ex);
                }
                if (photoFile != null) {
                    mCM = "file:" + photoFile.getAbsolutePath();
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                } else {
                    takePictureIntent = null;
                }
            }
            Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
            contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
            //contentSelectionIntent.setType("*/*");
            contentSelectionIntent.setType("image/*");
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            Intent[] intentArray;
            if (takePictureIntent != null) {
                intentArray = new Intent[]{takePictureIntent, intent};
            } else {
                intentArray = new Intent[0];
            }

            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_image));
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
            startActivityForResult(chooserIntent, FILE_CHOOSER);
            return true;
        }
    });

This code works if I DO NOT put this permission to the AndroidManifest:

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

If I add this permission to the manifest file the camera does not work anymore.

Upvotes: 1

Views: 65

Answers (2)

Priyank-py
Priyank-py

Reputation: 369

In android M+ first check if user have given the permission if not you have to request it:

private static final String REQUEST_CODE = 123;

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_DENIED) {
    requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE);
}

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006674

That is working as intended.

You only need that permission if your app is using the camera APIs in your own process, whether directly or through a library. Your code is not using the camera APIs, but instead is invoking some third-party camera app via ACTION_IMAGE_CAPTURE. So, you do not need that permission.

However, if your app does request the CAMERA permission in the manifest, you have to request it at runtime as well on Android 6.0+ devices. Otherwise, you cannot use ACTION_IMAGE_CAPTURE either.

This is covered in the documentation.

Upvotes: 1

Related Questions