将fileChooserParams中的mime类型转换为Intent.setType

时间:2019-04-01 03:22:37

标签: java android ionic-framework webview filepicker

我正在尝试使用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触发此方法的执行时,作为参数传入的mime类型fileChooserParams为:[.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贡献者的cordova-android中,如何安全地转换 fileChooserParams 正确的格式?

2 个答案:

答案 0 :(得分:0)

我决定在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

我希望它会被接受。

答案 1 :(得分:0)

我使用以下代码改进了解决方案:

   // 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