Reputation: 97
I have an issue guys. I have a webView
app. Inside the app is a button that should enable user pick file from device and upload to api. Everything was handled with the web. The problem is that on the webView
app, on tapping the button, nothing happens, however, when I try with a chrome browser or a browser app, it works well.
What do I need to do to ensure it works the same way as the chrome browser? I don't want to handle this functionality natively as that's the only solutions I saw online. I tried doing that anyways, but it doesn't pop out those file options like in this image
Is there an easier way to handle this?
Upvotes: 5
Views: 5830
Reputation: 484
You have to set the webview like,
// 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);
and need to set up custom `WebViewClient, check out the link for more details. Open file chooser in webview.
Let me know if it is working for you or not.
Upvotes: 0
Reputation: 71
Try this:
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)
[email protected] = 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
}
}
}
}
Upvotes: 7