Android 4.4 WebView file chooser not opening?

后端 未结 9 565
南旧
南旧 2020-12-14 07:25

We are creating an app which uses the webview and will access a page where the user needs to upload a file. We are experiencing problems with Android 4.4 where the file choo

9条回答
  •  余生分开走
    2020-12-14 07:54

    Android Kotlin,

    MyWebViewChromeClient class:

    class MyWebViewChromeClient(private val mContext: MyMainActivity): WebChromeClient() {
    
        override fun onShowFileChooser(webView: WebView, filePathCallback: ValueCallback>, fileChooserParams: FileChooserParams): Boolean {
            mContext.mUploadMessageArray?.onReceiveValue(null)
            mContext.mUploadMessageArray = filePathCallback
    
            val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
            contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
            contentSelectionIntent.type = "*/*"
            val intentArray: Array = arrayOfNulls(0)
    
            val chooserIntent = Intent(Intent.ACTION_CHOOSER)
            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "File Chooser")
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
            mContext.startActivityForResult(chooserIntent, mContext.FILECHOOSER_RESULTCODE)
            return true
        }
    

    MyMainActivity class:

    class MyMainActivity : MyBaseActivity() {
    
        val FILECHOOSER_RESULTCODE = 1001
        var mUploadMessageArray: ValueCallback>? = null
    
        private lateinit var mWebView: MyWebView
        private lateinit var mContext: Context
    
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            if (requestCode == FILECHOOSER_RESULTCODE) {
                if (mUploadMessageArray == null) {
                    return
                }
                val result = if (intent == null || resultCode != Activity.RESULT_OK) null else data?.data
                result?.let {
                    val uriArray: Array = arrayOf(it)
                    mUploadMessageArray?.onReceiveValue(uriArray)
                    mUploadMessageArray = null
                } ?: kotlin.run {
                    mUploadMessageArray?.onReceiveValue(null)
                    mUploadMessageArray = null
                }
            }
        }
    }
    

提交回复
热议问题