How to do a FileChooser into a WebView Android App

80 Views Asked by At

I'm making a WebView within an application to respond to a JotForm form, this form has a button to send files, how do I make it so that when the button is clicked, the application opens the device's file explorer? this is my Kotlin Code

package com.example.orgs
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.KeyEvent
import android.view.Window
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity

class SecondActivity : AppCompatActivity() {
    private lateinit var view: WebView

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
            view.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.activity_second)
        view = findViewById(R.id.webView)
        view.settings.javaScriptEnabled = true
        view.settings.builtInZoomControls = false
        view.webViewClient = MyWebClient()
        view.loadUrl("https://form.jotform.com/230447298445059")
        view.webChromeClient = WebChromeClient()
    }

    private inner class MyWebClient : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            if (url?.startsWith("tel:") == true || url?.startsWith("whatsapp:") == true) {
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                startActivity(intent)
                return true
            }
            return false
        }
    }
}

2

There are 2 best solutions below

1
Shubham Kumar Mahato On

You must include a JavaScript interface in your WebView that can be called from the web page if you want the button on the JotForm form to launch the device's file explorer when it is clicked. With this interface, you'll be able to respond to messages sent from the website by taking actions in your app.

1
Almir Gabriel On

you say something like that?

view.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                view?.evaluateJavascript(
                    """
                        (function() {
                            var button = document.getElementById('input_12');
                            button.addEventListener('click', function() {
                                if (document.getElementById('input')) {
                                    document.getElementById('input').click();
                                } else {
                                    alert('Error!!');
                                }
                            });
                        })();
                    """.trimIndent(), null)
            }