I am trying to upload a file using a WebView in Android.
This is the code in use:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
Intent intent = fileChooserParams.createIntent();
LOG.d(LOG_TAG, "mime types: " + Arrays.toString(fileChooserParams.getAcceptTypes()));
// PRINTS [.jpg,.png,.tiff,.jpeg,.tif,.pdf] !!
try {
parentEngine.cordova.startActivityForResult(new CordovaPlugin() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// ...
}
}, intent, FILECHOOSER_RESULTCODE);
} catch (ActivityNotFoundException e) {
// ...
}
return true;
}
The problem is that when the external library that I must use (ng-file-upload
) triggers the execution of this method, the mime types passed as argument in fileChooserParams are: [.jpg,.png,.tiff,.jpeg,.tif,.pdf]
. I don't see most of these in the list of allowed mime types.
As a consequence I find this error in LogCat:
No activity found to handle file chooser intent.: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT cat=[android.intent.category.OPENABLE] typ=.jpg,.png,.tiff,.jpeg,.tif,.pdf }
If I simply add intent.setType("image/* application/pdf");
everything works as expected!
Now the question is: in the Merge Request that I want to submit to cordova-android's contributors how do I safely transform the fileChooserParams to the correct format?
I improved the solution using this code:
See my Pull Request for more details.
I resolved simply adding the following 2 lines in my PR:
Outcome:
2019-04-01 00:18:00.501 32500-32500/my.app.bundle.id D/SystemWebChromeClient: : fileChooserParams.getAcceptTypes(): [.jpg,.png,.tiff,.jpeg,.tif,.pdf]
2019-04-01 00:18:00.503 2225-2921/system_process I/ActivityManager: START u0 {act=android.intent.action.GET_CONTENT cat=[android.intent.category.OPENABLE] typ=image/* cmp=com.android.documentsui/.picker.PickActivity (has extras)} from uid 10105
I hope it will get accepted.