我需要开发一个小型电子邮件应用程序,我必须生成 CSV 并使用 android 发送该 CSV 文档。我必须发送此附件而不保存到存储中。我发现使用文件编写器和我的代码附加 CSV 文件很困难,如下所示:
@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;
}
}
我在这一行遇到了问题:
email.putExtra(Intent.EXTRA_STREAM,generateCsvFile("testdata.csv"));