Skip to content

Commit b7d2b8c

Browse files
committed
Better type defaults and detection
1 parent 0a558d1 commit b7d2b8c

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/android/FileViewerPlugin.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.util.Log;
1616
import android.text.Html;
1717
import android.webkit.MimeTypeMap;
18+
import java.net.HttpURLConnection;
1819

1920
import org.apache.cordova.CordovaActivity;
2021
import org.apache.cordova.CallbackContext;
@@ -71,10 +72,18 @@ public class FileViewerPlugin extends CordovaPlugin {
7172
int x = uri.toString().lastIndexOf('.');
7273
if (x > 0) {
7374
ext = uri.toString().substring(x+1);
75+
//ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
7476
}
75-
// Log.d(LOG_TAG, ext);
76-
String type = obj.has("type") ? obj.getString("type") : mime.getMimeTypeFromExtension(ext);
77-
77+
Log.d(LOG_TAG, ext);
78+
String type = obj.has("type") ? obj.getString("type") : mime.getMimeTypeFromExtension(ext.toLowerCase());
79+
if (type == null) {
80+
type = HttpURLConnection.guessContentTypeFromName(uri.toString());
81+
}
82+
83+
if (type == null) {
84+
type = "application/" + ext;
85+
}
86+
7887
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
7988
Map<String, String> extrasMap = new HashMap<String, String>();
8089

@@ -161,11 +170,16 @@ void view(String action, Uri uri, String type,
161170
((CordovaActivity)this.cordova.getActivity()).startActivity(i);
162171
} catch (Exception ex) {
163172
ex.printStackTrace();
164-
callbackContext.error("Error. No Activity found to handle Intent.");
173+
if (!type.equals("*/*")) {
174+
// Try the fallback if we haven't already
175+
view(action, uri, "*/*", extras, callbackContext);
176+
} else {
177+
callbackContext.error("Error. No Activity found to handle Intent.");
178+
}
165179
}
166180
}
167181

168-
void share(String action, String type, Map<String, String> extras,
182+
void share(String action, String type, Map<String, String> extras,
169183
CallbackContext callbackContext) {
170184
try {
171185
Intent i = new Intent(action);
@@ -194,7 +208,10 @@ void share(String action, String type, Map<String, String> extras,
194208
ext = uri.toString().substring(x+1);
195209
}
196210
// Log.d(LOG_TAG, ext);
197-
String calculatedType = mime.getMimeTypeFromExtension(ext);
211+
String calculatedType = mime.getMimeTypeFromExtension(ext.toLowerCase());
212+
if (calculatedType == null) {
213+
calculatedType = "*/*";
214+
}
198215
i.setType(calculatedType);
199216
} else if (key.equals(Intent.EXTRA_EMAIL)) {
200217
// allows to add the email address of the receiver

0 commit comments

Comments
 (0)