Taking picture/photo with webview

Viewed 508

I've the below code that is opening smoothly at mobile browser, and give me the options either to take a photo with the camera, or to upload photo from the gallery:

<input type="file" accept="image/*;capture=camera"/>

I'm loading the same at WebView, and made the settings as below:

webView.webChromeClient = object : WebChromeClient() {

            override fun onShowFileChooser(
                webView: WebView,
                filePathCallback: ValueCallback<Array<Uri>>,
                fileChooserParams: FileChooserParams
            ): Boolean {
                var takePictureIntent: Intent? = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
                contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
                contentSelectionIntent.type = "image/*"
                if (ContextCompat.checkSelfPermission(
                        ctx,
                        Manifest.permission.CAMERA
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    ActivityCompat.requestPermissions(
                        activity,
                        arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
                        0
                    )
                }

                val intentArray: Array<Intent?> = takePictureIntent?.let { arrayOf(it) } ?: arrayOfNulls(0)

                val chooserIntent = Intent(Intent.ACTION_CHOOSER)
                chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
                chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser")
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
                startActivity(chooserIntent)

                filePathCallback?.onReceiveValue(null)
                return true
            }
}

I'm getting the Image chooser open correctly as below:

enter image description here

But, I've the below questins:

  1. If I took a photo by the camera how can I return the new photo path to the webview?
  2. If I picked a photo from the gallery, how can I retun the path to the webview?
0 Answers
Related