Compress image in webview before upload android kotlin

Viewed 45

I have created webview view in Android Kotlin with two options i.e. by opening file manager or camera help me add it before uploading from both options and compress data directly so the image size is small

following code for webChromeClient()

binding.webview.webChromeClient = object : WebChromeClient() {
            @SuppressLint("QueryPermissionsNeeded")
            override fun onShowFileChooser(
                view: WebView,
                filePath: ValueCallback<Array<Uri>>,
                fileChooserParams: FileChooserParams
            ): Boolean {
                mFilePathCallback?.onReceiveValue(null)
                mFilePathCallback = filePath
                var takePictureIntent: Intent? = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                if (takePictureIntent!!.resolveActivity(packageManager) != null) {
                    var photoFile: File? = null
                    try {
                        photoFile = createImageFile()
                        takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath)
                    } catch (ex: IOException) {
                        Log.e(TAG, "Unable to create Image File", ex)
                    }
                    if (photoFile != null) {
//                        val stream = contentResolver.openInputStream(Uri.fromFile(photoFile))
//                        val bitmap = BitmapFactory.decodeStream(stream)
//                        val baos = ByteArrayOutputStream()
//                        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos)
//                        val path = MediaStore.Images.Media.insertImage(contentResolver, bitmap, "Title", null)
                        mCameraPhotoPath = "file:" + photoFile.absolutePath
                        takePictureIntent.putExtra(
                            MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile)
                        )
                    } else {
                        takePictureIntent = null
                    }
                }
                val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
                contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
                contentSelectionIntent.type = "image/*"
                val intentArray: Array<Intent?> = takePictureIntent?.let { arrayOf(it) } ?: arrayOfNulls(0)
                val chooserIntent = Intent(Intent.ACTION_CHOOSER)
                chooserIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
                chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
                chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser")
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
                startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE)
                return true
            }
        }

Here's for the onActivityResult() function

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
        super.onActivityResult(requestCode, resultCode, data)
        return
    }
    var results: Array<Uri>? = null
    if (resultCode == RESULT_OK) {
        if (data == null) {
            if (mCameraPhotoPath != null) {
                results = arrayOf(Uri.parse(mCameraPhotoPath))
            }
        } else {
            val dataString = data.dataString
            if (dataString != null) {
                results = arrayOf(Uri.parse(dataString))
            }
        }

    }
    mFilePathCallback!!.onReceiveValue(results)
    mFilePathCallback = null

   //        val newPath: String?
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage) {
            return
        }
        val result: Uri?
        if (resultCode != RESULT_OK) {
            result = null
        } else {
            result = if (intent == null) mCapturedImageURI else intent.data
        }

        val storageDir = 
  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
    //            if (!storageDir.exists()){
    //                storageDir.mkdirs();
     //            }
        val file = File(
            storageDir, File.separator.toString() + "IMG_" + System.currentTimeMillis()
                .toString() + ".jpg"
        )

        val newPath = getRealPathFromURI(this, result)
        //            newPath = if (result == mCapturedImageURI) {
        //                file.getAbsolutePath()
        //            } else {
        //                getRealPathFromURI(applicationContext, result)
        //            }
        val bMap = BitmapFactory.decodeFile(newPath)
        val out = Bitmap.createScaledBitmap(bMap, 150, 150, false)
        val resizedFile = File(storageDir, "resize.png")
        var fOut: FileOutputStream? = null
        try {
            fOut = FileOutputStream(resizedFile)
            out.compress(Bitmap.CompressFormat.JPEG, 50, fOut)
            fOut.flush()
            fOut.close()
            bMap.recycle()
            out.recycle()
        } catch (e: Exception) {
        }
        mUploadMessage!!.onReceiveValue(Uri.fromFile(resizedFile))
        mUploadMessage = null
   //            resizedFile.delete();
    }
    return

}

when running the code, the actual size of the running image, how to make it smaller or compress it before uploading

0 Answers
Related