Skip to content

Commit

Permalink
Merge pull request #274 from cinthiaro/master
Browse files Browse the repository at this point in the history
Open file in Android when the mime type is not specified
  • Loading branch information
shnist authored Oct 29, 2019
2 parents 5834ed4 + 82f9871 commit dac2053
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ this software and associated documentation files (the "Software"), to deal in
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.webkit.MimeTypeMap;

import io.github.pwlin.cordova.plugins.fileopener2.FileProvider;

Expand Down Expand Up @@ -102,6 +103,10 @@ private void _open(String fileArg, String contentType, Boolean openWithDefault,
File file = new File(fileName);
if (file.exists()) {
try {
if (contentType == null || contentType.trim().equals("")) {
contentType = _getMimeType(fileName);
}

Intent intent;
if (contentType.equals("application/vnd.android.package-archive")) {
// https://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider/9672282#9672282
Expand Down Expand Up @@ -151,6 +156,18 @@ private void _open(String fileArg, String contentType, Boolean openWithDefault,
}
}

private String _getMimeType(String url) {
String mimeType = "*/*";
int extensionIndex = url.lastIndexOf('.');
if (extensionIndex > 0) {
String extMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(url.substring(extensionIndex+1));
if (extMimeType != null) {
mimeType = extMimeType;
}
}
return mimeType;
}

private void _uninstall(String packageId, CallbackContext callbackContext) throws JSONException {
if (this._appIsInstalled(packageId)) {
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
Expand Down

0 comments on commit dac2053

Please sign in to comment.