|
15 | 15 | import android.util.Log;
|
16 | 16 | import android.text.Html;
|
17 | 17 | import android.webkit.MimeTypeMap;
|
| 18 | +import java.net.HttpURLConnection; |
18 | 19 |
|
19 | 20 | import org.apache.cordova.CordovaActivity;
|
20 | 21 | import org.apache.cordova.CallbackContext;
|
@@ -71,10 +72,18 @@ public class FileViewerPlugin extends CordovaPlugin {
|
71 | 72 | int x = uri.toString().lastIndexOf('.');
|
72 | 73 | if (x > 0) {
|
73 | 74 | ext = uri.toString().substring(x+1);
|
| 75 | + //ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); |
74 | 76 | }
|
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 | + |
78 | 87 | JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
|
79 | 88 | Map<String, String> extrasMap = new HashMap<String, String>();
|
80 | 89 |
|
@@ -161,11 +170,16 @@ void view(String action, Uri uri, String type,
|
161 | 170 | ((CordovaActivity)this.cordova.getActivity()).startActivity(i);
|
162 | 171 | } catch (Exception ex) {
|
163 | 172 | 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 | + } |
165 | 179 | }
|
166 | 180 | }
|
167 | 181 |
|
168 |
| - void share(String action, String type, Map<String, String> extras, |
| 182 | + void share(String action, String type, Map<String, String> extras, |
169 | 183 | CallbackContext callbackContext) {
|
170 | 184 | try {
|
171 | 185 | Intent i = new Intent(action);
|
@@ -194,7 +208,10 @@ void share(String action, String type, Map<String, String> extras,
|
194 | 208 | ext = uri.toString().substring(x+1);
|
195 | 209 | }
|
196 | 210 | // 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 | + } |
198 | 215 | i.setType(calculatedType);
|
199 | 216 | } else if (key.equals(Intent.EXTRA_EMAIL)) {
|
200 | 217 | // allows to add the email address of the receiver
|
|
0 commit comments