我无法在kotlin webview上传图像,视频和文件

时间:2018-03-08 07:25:26

标签: android file-upload webview kotlin image-upload

我可以选择视频和图片,但无法在我的webviewkotlin计划中上传。

webview.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, PICKFILE_REQUEST_CODE)
                    return true
                }
            })

            fun onActivityResult(requestCode: Int, resultCode: Int,
                                 intent: Intent,
                                 mFilePathCallback: Any): Boolean {
                var PICKFILE_REQUEST_CODE = null
                if (requestCode == PICKFILE_REQUEST_CODE)
                {
                    val result = if (intent == null || resultCode != RESULT_OK)
                        null
                    else
                        intent.getData()
                    val resultsArray = arrayOfNulls<Uri>(1)
                    resultsArray[0] = result
                    mFilePathCallback.onReceiveValue(resultsArray)

                }
                return true
            }

2 个答案:

答案 0 :(得分:0)

您需要添加到@Override onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
  if (requestCode == REQUEST_SELECT_FILE) {
        if (null == uploadMessage) {
            return
        }
        uploadMessage?.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode,data))
        uploadMessage = null

    }
}

但不在你的&#34; webview.setWebChromeClient内部(对象:WebChromeClient()&#34; hier不会覆盖。

答案 1 :(得分:0)

类MainActivity:AppCompatActivity(){

val TAG = "hellow web view"
val webURL: String = "https://www.google.com"


override fun onCreate(savedInstanceState: Bundle?): Unit {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    if (savedInstanceState == null) {
        webview.restoreState(savedInstanceState)
        // Log.i(TAG,"onCreate")
        webview.webViewClient = WebViewClient()
        webview.loadUrl(webURL)
        webview.settings.javaScriptEnabled = true
        webview.settings.builtInZoomControls = true
        webview.settings.displayZoomControls = true
        webview.settings.allowFileAccess = true
        webview.settings.allowFileAccessFromFileURLs = true

        webview.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, PICKFILE_REQUEST_CODE)
                return true
            }
        })

        fun onActivityResult(requestCode: Int, resultCode: Int,
                             intent: Intent,
                             mFilePathCallback: Any): Boolean {
            var PICKFILE_REQUEST_CODE = null
            if (requestCode == PICKFILE_REQUEST_CODE)
            {
                val result = if (intent == null || resultCode != RESULT_OK)
                    null
                else
                    intent.getData()
                val resultsArray = arrayOfNulls<Uri>(1)
                resultsArray[0] = result
                mFilePathCallback.onReceiveValue(resultsArray)

            }
            return true
        }



        webview.setDownloadListener(object : DownloadListener {
            override fun onDownloadStart(url: String, userAgent: String,
                                         contentDisposition: String, mimetype: String,
                                         contentLength: Long) {
                val request = DownloadManager.Request(Uri.parse(url))
                request.allowScanningByMediaScanner()

                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) //Notify client once download is completed!
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mimetype)
                val webview = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
                webview.enqueue(request)
                Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show()
            }
        })

        class webviewclient : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                webview.loadUrl("http://google.com")
                return true
            }
        }
        searchbtn.setOnClickListener({ (webview.loadUrl("https://www.google.com")) })

        btn1.setOnClickListener({ (webview.goBack()) })

        btn3.setOnClickListener({ (webview.goForward()) })
    }
}

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

override fun onSaveInstanceState(outState: Bundle?) {
    super.onSaveInstanceState(outState)
    webview.saveState(outState)
    Log.i(TAG, "onSaveInstanceState")
}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
    super.onRestoreInstanceState(savedInstanceState)
    webview.restoreState(savedInstanceState)
    Log.i(TAG, "onRestoreInstanceState")
}


private fun Any.onReceiveValue(resultsArray: Array<Uri?>) {}

}