欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

H5调用原生资源选择照片 input type=file

程序员文章站 2022-07-02 11:47:39
H5调用原生资源选择照片 input type=file提示:如果需要同时调用拍照或者选择照片,则自定义弹出框private var mUploadMessage: ValueCallback? = nullvar uploadMessage: ValueCallback>? = nullval REQUEST_SELECT_FILE = 100private val FILECHOOSER_RESULTCODE = 2.....

  H5调用原生资源选择照片 input type=file

提示:如果需要同时调用拍照或者选择照片,则自定义弹出框

private var mUploadMessage: ValueCallback<Uri>? = null
var uploadMessage: ValueCallback<Array<Uri>>? = null
val REQUEST_SELECT_FILE = 100
private val FILECHOOSER_RESULTCODE = 2

 

web_view.settings.javaScriptEnabled = true


web_view.webChromeClient = object : WebChromeClient() {

            override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
                if (uploadMessage != null) {
                    uploadMessage!!.onReceiveValue(null)
                    uploadMessage = null
                }

                uploadMessage = filePathCallback

                val intent = fileChooserParams!!.createIntent()
                try {
                   
                    startActivityForResult(intent, REQUEST_SELECT_FILE)

                } catch (e: ActivityNotFoundException) {
                    uploadMessage = null
                    Toast.makeText(baseContext, "Cannot Open File Chooser", Toast.LENGTH_LONG).show()
                    return false
                }
                return true
            }
        }

  
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (requestCode == REQUEST_SELECT_FILE) {
                if (uploadMessage == null)
                    return
                uploadMessage!!.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent))
                uploadMessage = null
            }
        } else if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return
            val result = if (intent == null || resultCode != RESULT_OK) null else intent.data
            mUploadMessage!!.onReceiveValue(result)
            mUploadMessage = null

        } else
            Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show()
    }

 

本文地址:https://blog.csdn.net/u010194271/article/details/110644987

相关标签: Kotlin