我有一个问题。我有一个webView
应用。应用程序内部是一个按钮,该按钮应允许用户从设备中选择文件并上传到api。一切都由网络处理。问题是在webView
应用程序上,点击按钮时,什么也没有发生,但是,当我尝试使用chrome浏览器或浏览器应用程序时,它运行良好。
我需要怎么做才能确保其与chrome浏览器的工作方式相同?我不想本机处理此功能,因为这是我在网上看到的唯一解决方案。无论如何,我都尝试这样做,但是它不会弹出this image
中的那些文件选项有没有更简单的方法来解决这个问题?
答案 0 :(得分:0)
尝试一下:
private var filePathCallback: ValueCallback<Array<Uri>>? = null
override fun onCreate(savedInstanceState: Bundle?) {
webView.webChromeClient = object: WebChromeClient() {
override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
startActivityForResult(fileChooserParams?.createIntent(), CHOOSE_FILE_REQUEST_CODE)
this@MainActivity.filePathCallback = filePathCallback
return true
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
CHOOSE_FILE_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
filePathCallback?.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data))
filePathCallback = null
}
}
}
}
答案 1 :(得分:0)
您必须将网络视图设置为
// Other webview options
webView.getSettings().setLoadWithOverviewMode(true);
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
//webView.getSettings().setUseWideViewPort(true);
//Other webview settings
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setSupportZoom(true);
,并且需要设置自定义`WebViewClient,请查看链接以获取更多详细信息。 Open file chooser in webview。
让我知道它是否对您有用。