Search code examples
javaandroidionic-frameworkwebviewfilepicker

Convert mime types in fileChooserParams to the right format for Intent.setType


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?


Solution

  • I improved the solution using this code:

       // Validation utility for mime types
        private List<String> extractValidMimeTypes(String[] mimeTypes) {
            List<String> results = new ArrayList<String>();
            List<String> mimes;
            if (mimeTypes.length() == 1 && mimeTypes[0].contains(",")) {
                mimes = Arrays.asList(mimeTypes[0].split(","));
            } else {
                mimes = Arrays.asList(mimeTypes);
            }
            MimeTypeMap mtm = MimeTypeMap.getSingleton();
            for (String mime : mimes) {
                if (mime != null && mime.trim().startsWith(".")) {
                    String extensionWithoutDot = mime.trim().substring(1, mime.trim().length());
                    String derivedMime = mtm.getMimeTypeFromExtension(extensionWithoutDot);
                    if (derivedMime != null && !results.contains(derivedMime)) {
                        // adds valid mime type derived from the file extension
                        results.add(derivedMime);
                    }
                } else if (mtm.getExtensionFromMimeType(mime) != null && !results.contains(mime)) {
                    // adds valid mime type checked agains file extensions mappings
                    results.add(mime);
                }
            }
            return results;
        }
    
    
    public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
        Intent intent = fileChooserParams.createIntent();
        List<String> validMimeTypes = extractValidMimeTypes(fileChooserParams.getAcceptTypes());
        if (validMimeTypes.isEmpty()) {
            intent.setType(DEFAULT_MIME_TYPE);
        } else {
            intent.setType(String.join(" ", validMimeTypes));
        }
        ...
    

    See my Pull Request for more details.