我在将txt文件附加到gmail时遇到了一些麻烦。 我调用了saveItToText()方法,它从字符串值创建一个txt文件,并调用sendEmail()将生成的txt文件附加到gmail,但是,当我打开gmail应用程序时,它说“无法附加空文件”。我可能在下面的代码中的逻辑中有一些错误。我想要做的就是将包含字符串的文件附加到电子邮件中。我在网上搜索但找不到一个好的解决方案。我很乐意听到你的消息!
public void saveItToText(Context context, String sFileName, String sBody) {
try {
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
, mFileName);
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
android.util.Log.d("Test","gpx "+root.toString());
FileWriter writer = new FileWriter(root);
writer.append(sBody);
writer.flush();
writer.close();
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendEmail(String email) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
, mFileName);
Uri path = Uri.fromFile(file);
android.util.Log.d("Test", "path: "+path.toString());
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/octet-stream");
//intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
String to[] = { email };
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_TEXT, mUrl);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivityForResult(Intent.createChooser(intent, "Send mail..."),
1222);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1222) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
, mFileName);
file.delete();
}
}