将 fileChooserParams 中的 mime 类型转换为 Intent.setType 的正确格式

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

我正在尝试使用 Android 中的 WebView 上传文件。

这是 the code 在使用:

@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;
}

问题是当我必须使用的外部库(ng-file-upload)触发这个方法的执行,在 fileChooserParams 中作为参数传递的 mime 类型是:[.jpg,.png,.tiff,.jpeg,.tif,.pdf]。我在 list of allowed mime types.

中看不到其中的大部分内容

因此我在 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 }

如果我简单地添加 intent.setType("image/* application/pdf"); 一切都会按预期进行!

现在 问题 是:在 Merge Request that I want to submit to cordova-android 的贡献者中,我如何安全地将 fileChooserParams 转换为正确的格式?

我决定在 my PR 中简单地添加以下两行:

intent.setType("image/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, fileChooserParams.getAcceptTypes());

结果:

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

我希望它能被接受。

我使用这段代码改进了解决方案:

   // 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));
    }
    ...

有关详细信息,请参阅我的 Pull Request