Reputation: 10622
I need to develop a small Email Application which i have to generate CSV and mail that CSV document using android. I have to send this attachment without saving into storage. I found difficult in attaching the CSV file using file writer and my code as follows :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
//email.putExtra(Intent.EXTRA_CC, new String[]{ to});
//email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.setType("text/csv");
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
email.putExtra(Intent.EXTRA_STREAM,generateCsvFile("testdata.csv"));
//need this to prompts email client only
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
});
}
public static FileWriter generateCsvFile(String sFileName) throws IOException
{
FileWriter writer;
writer = new FileWriter(sFileName);
writer.append("DisplayName");
writer.append(',');
writer.append("Age");
writer.append('\n');
writer.append("RajeshV");
writer.append(',');
writer.append("26");
writer.append('\n');
writer.append("Mrithula");
writer.append(',');
writer.append("29");
writer.append('\n');
return writer;
}
}
I got problem in this line :
email.putExtra(Intent.EXTRA_STREAM,generateCsvFile("testdata.csv"));
Upvotes: 2
Views: 8455
Reputation: 5
Intent jj=new Intent(android.content.Intent.ACTION_SEND);
Intent jj=new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
String fileName = "file://" + Environment.getExternalStorageDirectory()+"/"+filename;
jj.putExtra(Intent.EXTRA_SUBJECT, "Chat Info");
jj.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));
jj.setType("message/rsc822");
Intent chooser = Intent.createChooser(jj , "Select Sender");
context.startActivity(chooser);
Upvotes: 0
Reputation: 185
i've done for send any file from SD card with mail attachment..
Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("rar/image");
sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new
File("/mnt/sdcard/download/abc.rar")));
startActivity(Intent.createChooser(sendEmail, "Email:"));
Upvotes: 1
Reputation: 8645
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
//email.putExtra(Intent.EXTRA_CC, new String[]{ to});
//email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
//need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
});
}
}
Upvotes: 1
Reputation: 10622
I got Working Code
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");
File data = null;
try {
Date dateVal = new Date();
String filename = dateVal.toString();
data = File.createTempFile("Report", ".csv");
FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
data, "Name,Data1");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(i, "E-mail"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public class GenerateCsv {
public static FileWriter generateCsvFile(File sFileName,String fileContent) {
FileWriter writer = null;
try {
writer = new FileWriter(sFileName);
writer.append(fileContent);
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return writer;
}
}
Add this line in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-p
Upvotes: 2
Reputation: 3117
The are the steps to send the attachments
Intent email = new Intent(Intent.ACTION_SEND);
email .setType("image/jpeg");
email .putExtra(Intent.EXTRA_SUBJECT, "My Picture");
email .putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/captureimage.png"));
startActivity(Intent.createChooser(email , "Email:"));
For more information you can refer the following links
Upvotes: 2