From 9fc348abd3968c2a4ee8857ddfaba2eb52c34d9b Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Tue, 24 Sep 2019 15:27:35 +0200 Subject: [PATCH] fix(android): handle input file when accept have file extensions (#1990) --- .../getcapacitor/BridgeWebChromeClient.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/BridgeWebChromeClient.java b/android/capacitor/src/main/java/com/getcapacitor/BridgeWebChromeClient.java index d5834ddd1..e01968e1b 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/BridgeWebChromeClient.java +++ b/android/capacitor/src/main/java/com/getcapacitor/BridgeWebChromeClient.java @@ -12,6 +12,7 @@ import android.webkit.GeolocationPermissions; import android.webkit.JsPromptResult; import android.webkit.JsResult; +import android.webkit.MimeTypeMap; import android.webkit.PermissionRequest; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; @@ -303,8 +304,8 @@ private void showFilePicker(final ValueCallback filePathCallback, FileCho intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); } if (fileChooserParams.getAcceptTypes().length > 1) { - intent.setType("*/*"); - intent.putExtra(Intent.EXTRA_MIME_TYPES, fileChooserParams.getAcceptTypes()); + String[] validTypes = getValidTypes(fileChooserParams.getAcceptTypes()); + intent.putExtra(Intent.EXTRA_MIME_TYPES, validTypes); } try { bridge.cordovaInterface.startActivityForResult(new CordovaPlugin() { @@ -329,6 +330,24 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) { } } + private String[] getValidTypes(String[] currentTypes) { + List validTypes = new ArrayList<>(); + MimeTypeMap mtm = MimeTypeMap.getSingleton(); + for (String mime : currentTypes) { + if (mime.startsWith(".")) { + String extension = mime.substring(1); + String extensionMime = mtm.getMimeTypeFromExtension(extension); + if (extensionMime != null && !validTypes.contains(extensionMime)) { + validTypes.add(extensionMime); + } + } else if (!validTypes.contains(mime)) { + validTypes.add(mime); + } + } + Object[] validObj = validTypes.toArray(); + return Arrays.copyOf(validObj, validObj.length, String[].class); + } + @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { String tag = "Capacitor/Console";