|
|
Press the Send Email button to email an attached image.png file. At start of app, a directory is created on the sdcard at /mnt/sdcard/EmailAttachmentSend. The image.png file is copied from the app's resources drawable to the created directory. Upon send of the email, the To, CC, subject, message, and attached files are setup. Any number of To, CC emails can be applied as well as any number of attached files although only one file is attached. Android Intent ACTION_SEND_MULTIPLE is used to send the email. The intent pops-up a ContextMenu with options for GMail and SMS.
package com.brainyideas.dev.poc.eas;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.view.View.OnClickListener;
import android.view.View;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
public class EmailAttachmentSend extends Activity {
String ls;
String path;
String attachedPathFile;
Button button_report_submit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
void init() {
ls = System.getProperty("user.dir");
path = Environment.getExternalStorageDirectory().toString() + ls + "EmailAttachmentSend";
attachedPathFile = path + ls + "image.png";
ensurePath();
saveImageToSDCard();
button_report_submit = (Button) findViewById(R.id.email_send);
button_report_submit.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
// email the image file as an attachment
// you can add more comma separated emails
String[] emailTo = new String[] {"support@brainyideas.com"};
String[] emailCC = null;
String subject = "see attached image";
String message = "see attached image *.png file";
String[] filePaths = new String[1];
filePaths[0] = attachedPathFile;
sendEmail(
emailTo,
emailCC,
subject,
message,
filePaths);
}
});
}
void ensurePath() {
// create the target directory if it does not exist
File folder = new File(path);
if(folder.exists()) {
// ignore
} else {
folder.mkdir(); // returns true/false
}
}
void saveImageToSDCard() {
try {
InputStream is = getResources().openRawResource(R.drawable.image);
Bitmap bitmap = BitmapFactory.decodeStream(is);
FileOutputStream fos = new FileOutputStream(attachedPathFile);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendEmail(String[] emailAddresses, String[] carbonCopies, String subject, String message, String[] filePaths) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddresses);
emailIntent.putExtra(Intent.EXTRA_CC, carbonCopies);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");
// attach one file
ArrayList<Uri> uris = new ArrayList<Uri>();
for (String file : filePaths) {
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Email"));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/email_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Email"
android:layout_alignParentLeft="true"/>
</LinearLayout>