How to Attach Multiple Files to Email Client in Android

how to attach multiple files to email client in android

That works:

final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");

then add files' uris:

ArrayList<Uri> uris = new ArrayList<Uri>();

ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);

Hope that helps.

Android send email with multiple attachment using file provider

Found a solution with the following code :

 xml/provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

File baseDir = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOCUMENTS), "ABC");

if (!baseDir.exists()) {
baseDir.mkdirs();
}

Log.i("Debug", "baseDir " + baseDir.toString());
File f1 = new File(baseDir, "File1.txt");
writeToFile(f1, "This is file 1 contents 888888888");
Log.i("Debug", "f1 path" + f1.getAbsolutePath());
File f2 = new File(baseDir, "File2.txt");
writeToFile(f2, "This is file 2 contents 123456");
Log.i("Debug", "f2 path" + f2.getAbsolutePath());

String f1path = f1.toString();
String f2path = f2.toString();
string EXTRA_RECIPIENT = "janedoe@abc.com";
String message = "Test message 12345678 this is a test. ";
sendMail(f1path, f2path, message, EXTRA_RECIPIENT);

private void sendMail(String f1path, String f2path, String message, String mailTo) {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo});

File f1new = new File(f1path);
File f2new = new File(f2path);

ArrayList<Uri> uris = new ArrayList<Uri>();

uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", f1new ));
uris.add(FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", f2new ));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(intent);
}

Android Email intent with multiple attachments

I've solved it. When the images are chosen they are put into the ArrayList "userSelectedImageUriList" in onActivityResult.

Instead of parsing this list in the sendReport method, I just add this list to the intent.

The working method looks like this:

public void sendReport(Context context) {
EditText nameField = (EditText)findViewById(R.id.EditTextName);
EditText emailField = (EditText)findViewById(R.id.EditTextEmail);
EditText locationField = (EditText)findViewById(R.id.EditTextLocation);
EditText dateField = (EditText)findViewById(R.id.EditTextDate);
EditText bodyField = (EditText)findViewById(R.id.EditTextBody);

if(!nameField.getText().toString().matches("") && !emailField.getText().toString().matches("") && !locationField.getText().toString().matches("") && !dateField.getText().toString().matches("") && !bodyField.getText().toString().matches("")) {
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("report_email", emailField.getText().toString()).apply();
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("report_name", nameField.getText().toString()).apply();

String emailBody = "Name: "+nameField.getText()+"\n\n";
emailBody += "Location: "+locationField.getText()+"\n\n";
emailBody += "Time: "+dateField.getText()+"\n\n";
emailBody += "Description: "+bodyField.getText();

Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);

// set the type to 'email'
emailIntent.setType("text/plain");
String to[] = {emailField.getText().toString()};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);

// the attachment - ArrayList populated in onActivityResult
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, userSelectedImageUriList);

// the mail
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);

startActivity(Intent.createChooser(emailIntent , "Send with..."));
//finish();
} else {
Toast.makeText(this, getString(R.string.field_error), Toast.LENGTH_SHORT).show();
}
}

Send multiple attachments( images and PDFs) to gmail through intent

Code for file chooser:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
startActivityForResult(Intent.createChooser(intent, "ChooseFile"), PICKFILE_RESULT_CODE);

Code for intent:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, subarray);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("vnd.android.cursor.dir/email");
ArrayList<Uri> uris=new ArrayList<Uri>();
if (uriMap.size()!=0) {
Iterator i= uriMap.entrySet().iterator();
while(i.hasNext()){
Map.Entry mapElement = (Map.Entry) i.next();
uris.add((Uri)mapElement.getValue());
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
}
intent.setPackage("com.google.android.gm");
startActivityForResult(intent, 101);

using intent.setType() and intent.setPackage() worked for me!

sending an e-mail with multiple attachments

Try This its work fine.

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");

ArrayList<Uri> uris = new ArrayList<Uri>();

String[] filePaths = new String[] {image1 Path,image2 path};
for (String file : filePaths) {
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}

if ( !(app_preferences.getString("email", "") == null || app_preferences.getString("email", "").equals(""))) {
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {app_preferences.getString("email", "")});
}

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject name");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Please find the attachment.");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

startActivity(Intent.createChooser(emailIntent, "Email:"));

Unable to attach multiple files to an email in Android

Instead of Intent.ACTION_SEND I used Intent.ACTION_SEND_MULTIPLE and it worked.

From the developer documentation:

Activity Action: Deliver multiple data to someone else. Like ACTION_SEND, except the data is multiple.



Related Topics



Leave a reply



Submit