将fileChooserParams中的mime类型转换为Intent.setType的正确格式 [英] Convert mime types in fileChooserParams to the right format for Intent.setType
问题描述
我正在尝试使用Android中的WebView上传文件.
I am trying to upload a file using a WebView in Android.
这是
问题是,当我必须使用的外部库( The problem is that when the external library that I must use ( 因此,我在LogCat中发现此错误:
As a consequence I find this error in LogCat:
如果我只是添加 If I simply add 现在问题为:在合并请求我想提交给 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: 有关更多详细信息,请参见我的拉动请求. See my Pull Request for more details. 这篇关于将fileChooserParams中的mime类型转换为Intent.setType的正确格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!ng-file-upload
)触发该方法的执行时,在fileChooserParams中作为参数传递的MIME类型为:[.jpg,.png,.tiff,.jpeg,.tif,.pdf]
.我在允许的mime列表中看不到其中的大多数类型.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.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 }
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");
,一切都会按预期进行!intent.setType("image/* application/pdf");
everything works as expected!推荐答案
// 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));
}
...