Reputation: 51
I'm trying to open the input type="file"
file selector in a WebView
app on mobile, but the solutions given on other questions dealing with the same issue were for earlier android versions and don't seem to work on Android 9.
The problem seems to be with this specific piece of code right here:
//For Lollipop 5.0+ Devices:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try
{
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e)
{
uploadMessage = null;
Toast.makeText(MainActivity.this, "Error: Unable to open file browser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
Upvotes: 3
Views: 613
Reputation: 51
Fixed using this code here.
// For Lollipop 5.0+ Devices
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try
{
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*,video/*,audio/*,file/*");
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e)
{
uploadMessage = null;
Toast.makeText(MainActivity.this, "Error: Unable to open file browser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
Upvotes: 2