diff --git a/README.md b/README.md
index 5f6885e..93fe34c 100644
--- a/README.md
+++ b/README.md
@@ -44,6 +44,25 @@ OR
```
+## Sending Multiple Attachments
+
+You can now use this plugin to email multiple attachments as follows:
+
+```js
+ var email_attachments = [
+ "file:///storage/emulated/0/Android/data/com.your_company.your_app/files/file1.pdf",
+ "file:///storage/emulated/0/Android/data/com.your_company.your_app/files/file2.csv"
+ ];
+
+ var external_extras = {};
+ external_extras[window.plugins.webintent.EXTRA_STREAM] = email_attachments.join();
+ external_extras[window.plugins.webintent.EXTRA_EMAIL] = "";
+ external_extras[window.plugins.webintent.EXTRA_SUBJECT] = "My subject text";
+ external_extras[window.plugins.webintent.EXTRA_TEXT] = "My email body text";
+
+ window.plugins.webintent.startActivity({action: window.plugins.webintent.ACTION_SEND_MULTIPLE, extras: external_extras, type: "text/plain"}, function() {console.log("Success!");}, function() {console.log("Failure!");});
+```
+
## Disclaimers
- I'm not an Android nor Cordova Plugin developer, I have little knowledge on how Android and the plugin works, especially Intent. I just made this plugin works for my own purpose. I just copy & pasted code from other plugins to make it work. If you know how to clean up the code, please let me know.
diff --git a/src/android/WebIntent.java b/src/android/WebIntent.java
index 8fdf1d5..3790960 100644
--- a/src/android/WebIntent.java
+++ b/src/android/WebIntent.java
@@ -2,6 +2,7 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.ArrayList;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
@@ -174,10 +175,16 @@ public void onNewIntent(Intent intent) {
}
void startActivity(String action, Uri uri, String type, Map extras) {
+
+ if (uri != null) {
+ String uri_string = uri.toString();
+ Log.i(LOG_TAG, String.format("Start Activity : action = [%s], data = [%s]", action, uri_string));
+ }
+
//Kevin W: Android 7.0 Nougat no longer allow file to be shared by file://
//https://proandroiddev.com/sharing-files-though-intents-are-you-ready-for-nougat-70f7e9294a0b
- if(newApi() && uri.getScheme().equals("file")){
- try{
+ try{
+ if(newApi() && (uri != null) && uri.getScheme().equals("file")){
File fileToShare = new File(new URI(uri.toString()));
Uri contentUri = FileProvider.getUriForFile(this.cordova.getActivity(),
cordova.getActivity().getPackageName() + ".provider",
@@ -191,40 +198,56 @@ void startActivity(String action, Uri uri, String type, Map extr
((CordovaActivity)this.cordova.getActivity()).startActivity(shareIntent);
return;
- }catch(URISyntaxException e){
- final String errorMessage = e.getMessage();
- Log.e(LOG_TAG, errorMessage);
- this.onNewIntentCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, errorMessage));
- }
- }else{
- Intent i = uri != null ? new Intent(action, uri) : new Intent(action);
- if (type != null && uri != null) {
- i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
- } else {
- if (type != null) {
- i.setType(type);
- }
- }
-
- for (Map.Entry entry : extras.entrySet()) {
- final String key = entry.getKey();
- final String value = entry.getValue();
- // If type is text/html, the extra text must be sent as HTML.
- if (key.equals(Intent.EXTRA_TEXT) && "text/html".equals(type)) {
- i.putExtra(key, Html.fromHtml(value));
- } else if (key.equals(Intent.EXTRA_STREAM)) {
- // Allows sharing of images as attachments.
- // `value` in this case should be the URI of a file.
- final CordovaResourceApi resourceApi = webView.getResourceApi();
- i.putExtra(key, resourceApi.remapUri(Uri.parse(value)));
- } else if (key.equals(Intent.EXTRA_EMAIL)) {
- // Allows adding the email address of the receiver.
- i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
+ }else{
+ Intent i = uri != null ? new Intent(action, uri) : new Intent(action);
+ if (type != null && uri != null) {
+ i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
} else {
- i.putExtra(key, value);
+ if (type != null) {
+ i.setType(type);
+ }
}
+
+ for (Map.Entry entry : extras.entrySet()) {
+ final String key = entry.getKey();
+ final String value = entry.getValue();
+
+ // If type is text/html, the extra text must be sent as HTML.
+ if (key.equals(Intent.EXTRA_TEXT) && "text/html".equals(type)) {
+ i.putExtra(key, Html.fromHtml(value));
+ } else if (key.equals(Intent.EXTRA_STREAM)) {
+ if(action.equals(Intent.ACTION_SEND_MULTIPLE)) {
+ ArrayList extraUris = new ArrayList();
+ for(String extraUri : value.split(",")) {
+ final CordovaResourceApi resourceApi = webView.getResourceApi();
+ File fileToShare = new File(new URI(resourceApi.remapUri(Uri.parse(extraUri)).toString()));
+ Uri contentUri = FileProvider.getUriForFile(this.cordova.getActivity(),
+ cordova.getActivity().getPackageName() + ".provider",
+ fileToShare);
+ extraUris.add(contentUri);
+ }
+ i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, extraUris);
+ i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+ }
+ else {
+ // Allows sharing of images as attachments.
+ // `value` in this case should be the URI of a file.
+ final CordovaResourceApi resourceApi = webView.getResourceApi();
+ i.putExtra(key, resourceApi.remapUri(Uri.parse(value)));
+ }
+ } else if (key.equals(Intent.EXTRA_EMAIL)) {
+ // Allows adding the email address of the receiver.
+ i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
+ } else {
+ i.putExtra(key, value);
+ }
+ }
+ ((CordovaActivity)this.cordova.getActivity()).startActivity(i);
}
- ((CordovaActivity)this.cordova.getActivity()).startActivity(i);
+ }catch(URISyntaxException e){
+ final String errorMessage = e.getMessage();
+ Log.e(LOG_TAG, errorMessage);
+ this.onNewIntentCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, errorMessage));
}
}
diff --git a/www/webintent.js b/www/webintent.js
index 7120c25..a206f5c 100644
--- a/www/webintent.js
+++ b/www/webintent.js
@@ -9,6 +9,7 @@
};
WebIntent.prototype.ACTION_SEND = "android.intent.action.SEND";
+ WebIntent.prototype.ACTION_SEND_MULTIPLE = "android.intent.action.SEND_MULTIPLE";
WebIntent.prototype.ACTION_VIEW= "android.intent.action.VIEW";
WebIntent.prototype.EXTRA_TEXT = "android.intent.extra.TEXT";
WebIntent.prototype.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";