Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an email with attachments programmatically on Android

I wish to implement a button that upon pressing it will open the default email client with an attachment file.

I am following this, but am getting an error message on the startActivity, saying it is expecting an activity param while I am giving it an intent. I am using API 21 and Android Studio 1.1.0, so perhaps it has something to do with the comment in the answer provided in the link?

This is my fourth day as Android developer so sorry if I am missing something really basic.

Here is my code:

    public void sendFileToEmail(File f){

    String subject = "Lap times";
    ArrayList<Uri> attachments = new ArrayList<Uri>();
    attachments.add(Uri.fromFile(f));
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
    intent.setClassName("com.android.email", "com.android.mail.compose.ComposeActivity");

    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
like image 480
Digital Da Avatar asked Mar 02 '15 12:03

Digital Da


People also ask

Where are email attachments on Android?

Select a message with an attachment, then select the Download File icon below the attachment. The file will be saved to the "Downloads" section on your Android device.

What is send as attachment?

An email attachment is a computer file sent along with an email message. One or more files can be attached to any email message, and be sent along with it to the recipient. This is typically used as a simple method to share documents and images.


3 Answers

Official documentation with Kotlin snippets is here: https://developer.android.com/guide/components/intents-common#ComposeEmail

I think your problem is that you are not using the correct file path.

The following works for me:

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
String pathToMyAttachedFile = "temp/attachement.xml";
File file = new File(root, pathToMyAttachedFile);
if (!file.exists() || !file.canRead()) {
    return;
}
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));

EDIT: Requesting access to storage just to share a file private to your app is probably not a good idea. Fortunately, after a little configuration, it's very easy to share a file from your app private storage. See this guide: https://developer.android.com/training/secure-file-sharing/setup-sharing

If you share a file that is on external storage, you also need to give the user permission via a manifest file like below

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
like image 129
King of Masses Avatar answered Nov 04 '22 06:11

King of Masses


For newer devices you will encounter FileUriExposedException. Here is how to avoid it in Kotlin.

val file = File(Environment.getExternalStorageDirectory(), "this")
val authority = context.packageName + ".provider"
val uri = FileProvider.getUriForFile(context, authority, file)
val emailIntent = createEmailIntent(uri)
startActivity(Intent.createChooser(emailIntent, "Send email..."))

private fun createEmailIntent(attachmentUri: Uri): Intent {
    val emailIntent = Intent(Intent.ACTION_SEND)
    emailIntent.type = "vnd.android.cursor.dir/email"
    val to = arrayOf("[email protected]")
    emailIntent.putExtra(Intent.EXTRA_EMAIL, to)
    emailIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri)
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject")
    return emailIntent
}
like image 31
divonas Avatar answered Nov 04 '22 08:11

divonas


Try to use this.It is working...

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
                    emailIntent.setType("*/*");

                    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(listVideos.get(position).getVideoPath())));//path of video 
                    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Thanks

like image 35
Sulabh Jain Avatar answered Nov 04 '22 07:11

Sulabh Jain