将 fileChooserParams 中的 mime 类型转换为 Intent.setType 的正确格式 [英] Convert mime types in fileChooserParams to the right format for Intent.setType

查看:31
本文介绍了将 fileChooserParams 中的 mime 类型转换为 Intent.setType 的正确格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to upload a file using a WebView in Android.

这是正在使用的代码:

@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].我在 允许的 mime 列表中没有看到其中的大部分内容类型.

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.

因此,我在 LogCat 中发现了这个错误:没有找到处理文件选择器意图的活动.:android.content.ActivityNotFoundException:没有找到处理意图的活动{ act=android.intent.action.GET_CONTENT cat=[android.intent.category.OPENABLE] typ=.jpg,.png,.tiff,.jpeg,.tif,.pdf }

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 }

如果我只是添加 intent.setType("image/* application/pdf"); 一切都按预期工作!

If I simply add intent.setType("image/* application/pdf"); everything works as expected!

现在问题是:在合并请求 我想提交给 cordova-android 的贡献者如何我如何安全地将 fileChooserParams 转换为正确的格式?

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:

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

这篇关于将 fileChooserParams 中的 mime 类型转换为 Intent.setType 的正确格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆