Tuesday 26 October 2021

Android Webview Duplicate Files when Selecting Image

I'm having a problem with uploading files on android webview which occurs when I select a file or camera to select then a CAPUTURE_OOOOOOOO.jpg file is formed which should only be formed when I manage to take a photo and take the image into the selected image file, here's the code with kotlin

myWebView.webChromeClient = object : WebChromeClient() {
        override fun onShowFileChooser(webView: WebView, filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: FileChooserParams):Boolean {
            chooserCallback = filePathCallback
            
            val fileCamera = File(
                Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES
                ), "CAPTURE_" + System.currentTimeMillis().toString() + ".jpg"
            )
            chooserCamera = Uri.fromFile(fileCamera)

            val captureIntent =  Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, chooserCamera)

            val imageIntent =  Intent(Intent.ACTION_GET_CONTENT)
            imageIntent.addCategory(Intent.CATEGORY_OPENABLE)
            imageIntent.type = "image/*"

            val chooserIntent = Intent.createChooser(imageIntent, "Select Image")
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(captureIntent))
            startActivityForResult(chooserIntent, requestChooser)
            return true
        }
    }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == requestChooser) {
        if (resultCode == Activity.RESULT_OK) {
            Log.v("chooserCamera", data?.dataString.toString())
            Log.v("dataString", data?.dataString.toString())
            val result: Uri = if (data?.dataString == null)  chooserCamera as Uri else data.data as Uri
            chooserCallback?.onReceiveValue(arrayOf(result))
            chooserCallback = null
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(applicationContext, "No Image", Toast.LENGTH_SHORT).show()
            chooserCallback?.onReceiveValue(null)
            chooserCallback = null
        }
    }
}

the CAPUTURE_OOOOOOOO.jpg file should only be formed when I take a photo with the camera and select it, not when I press the choose button so there is a duplication of files in this process, thank you

video recorded from virtual devices - Nexus 5 Api 30 : https://streamable.com/0kylsu



from Android Webview Duplicate Files when Selecting Image

No comments:

Post a Comment