0

File chooser not work on my webview component. I can choose the file from storage but when i select it nothing happens. I can't figure out how achieve it. It would be wonderful if user can also decide to take a photo instead of pick existing one (as it happen on chrome).

I read a lot of example and guide but no one seem work. I'm using Kotlin and API level 30+ (for now i'm not interested for old android version support). I have permission for INTERNET, WRITE_EXTERNAL_STORAGE, ACCESS_NETWORK_STATE.

That's the code of my main activity:

import android.Manifest
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.*
import android.widget.Toast
import androidx.core.app.ActivityCompat
import android.webkit.WebView
import android.webkit.WebChromeClient


class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        checkDownloadPermission()

        val contentView: WebView = findViewById(R.id.webview)

        contentView.setWebChromeClient(object:WebChromeClient() {
            override fun onShowFileChooser(webView:WebView, filePathCallback:ValueCallback<Array<Uri>>, fileChooserParams:FileChooserParams):Boolean {
                var mFilePathCallback = filePathCallback
                val intent = Intent(Intent.ACTION_GET_CONTENT)
                intent.setType("*/*")
                val PICKFILE_REQUEST_CODE = 100
                startActivityForResult(Intent.createChooser(intent, "Select a file"), 111)
                return true
            }
        })

        contentView.setWebViewClient(WebViewClient())

        contentView.settings.javaScriptEnabled = true

        contentView.settings.allowFileAccess = true
        contentView.settings.domStorageEnabled = true
        contentView.settings.allowContentAccess = true

        contentView.settings.loadWithOverviewMode = true
        contentView.settings.useWideViewPort = true

        contentView.settings.setSupportZoom(true)
        contentView.settings.builtInZoomControls = true
        contentView.settings.displayZoomControls = false

        contentView.setForceDarkAllowed(false)

        contentView.loadUrl("**** MY WEB URL ****")

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 111 && resultCode == RESULT_OK) {
            val selectedFile = data?.data //The uri with the location of the file
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        val myWebView: WebView = findViewById(R.id.webview)
        // Check if the key event was the Back button and if there's history
        if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
            myWebView.goBack()
            return true
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event)
    }

    private fun checkDownloadPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(
                this@MainActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        ) {
            Toast.makeText(
                this@MainActivity,
                "Write External Storage permission allows us to save files. Please allow this permission in App Settings.",
                Toast.LENGTH_LONG
            ).show()
        } else {
            ActivityCompat.requestPermissions(
                this@MainActivity,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                100
            )
        }
    }

}

Please, someone can help me? Thanks in advice!

MMV96
  • 1
  • 1
  • I have found answer for a similar problem here: https://stackoverflow.com/questions/5907369/file-upload-in-webview/31606758#31606758 – buhao75 Feb 04 '22 at 07:35

0 Answers0