Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): handle input file when accept have file extensions #1990

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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