这里有一些关于堆栈溢出的问题,但是没有答案,如果是的话,那是在Java中而不是在Kotlin中。
我有一个用Kotlin编写的简单WebView应用程序,它可以打开我的Web应用程序。一切正常,但通过HTML表单上传文件时遇到问题。我想出了如何打开FileChooser,但是当我选择一个文件时,输入仍然没有值。
您遇到过同样的问题吗?
下面是我的代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activity_main)
webView.setWebViewClient(WebViewClient())
webView.settings.javaScriptEnabled = true
webView.settings.allowFileAccess = true
webView.settings.allowFileAccessFromFileURLs = true
webView.settings.setAllowFileAccess(true)
webView.settings.setAllowContentAccess(true)
webView.settings.setGeolocationEnabled(true)
webView.fitsSystemWindows = true
webView.settings.domStorageEnabled = true
webView.settings.setSupportMultipleWindows(true)
webView.settings.loadWithOverviewMode = true
webView.settings.allowContentAccess = true
webView.settings.setGeolocationEnabled(true)
webView.webChromeClient = (object:WebChromeClient() {
override fun onGeolocationPermissionsShowPrompt(origin: String?, callback: GeolocationPermissions.Callback?) {
callback!!.invoke(origin, true, false)
}
// onShowFileChooser
override fun onShowFileChooser(view: WebView, filePathCallback:ValueCallback<Array<Uri>>, fileChooserParams:FileChooserParams):Boolean {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("image/*")
val PICKFILE_REQUEST_CODE = 100
startActivityForResult(intent, PICKFILE_REQUEST_CODE)
return false
}
})
webView.settings.setRenderPriority(WebSettings.RenderPriority.HIGH)
webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}