Skip to content

Commit

Permalink
fix(android): handle input file when accept have file extensions (#1990)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Sep 24, 2019
1 parent 52f083d commit 9fc348a
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -303,8 +304,8 @@ private void showFilePicker(final ValueCallback<Uri[]> 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() {
Expand All @@ -329,6 +330,24 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
}
}

private String[] getValidTypes(String[] currentTypes) {
List<String> 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";
Expand Down

0 comments on commit 9fc348a

Please sign in to comment.