Android 生成csv文件和电子邮件作为附件
我正在尝试生成Csv文件,同时将其作为电子邮件附件发送。通过电子邮件发送时会显示附件,但问题是当我检查收件箱时,我只发现没有附件的电子邮件。我将感谢你的帮助 这是我尝试过的代码Android 生成csv文件和电子邮件作为附件,android,android-intent,email-attachments,Android,Android Intent,Email Attachments,我正在尝试生成Csv文件,同时将其作为电子邮件附件发送。通过电子邮件发送时会显示附件,但问题是当我检查收件箱时,我只发现没有附件的电子邮件。我将感谢你的帮助 这是我尝试过的代码 buttonSend = (Button) findViewById(R.id.buttonSend); textTo = (EditText) findViewById(R.id.editTextTo); textSubject = (EditText) findViewById(R
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;
}
}
可以看出,您的GenerateCsv不起作用,您必须导入csv库。在您的代码中没有任何。要下载,请检查以下内容: 另见: 舱单中的许可:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您对此感到高兴吗?我也有同样的问题!