我尝试将文件附加到电子邮件。 用户写入文件。 完成后,文件保存在内部存储器上,应通过电子邮件发送。如何将文件附加到电子邮件? - android
下面是代码:
// write text to file
public void WriteBtn(View v) {
// add-write text into file
try {
FileOutputStream fileout = openFileOutput(fileName, MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(textmsg.getText().toString());
outputWriter.close();
fileout.close();
String fileLocation= Environment.getDataDirectory()+"/"+fileName;
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"[email protected]"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, fileLocation);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
// Uri uri = Uri.fromFile(new File(fileLocation));
String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
Uri uri = Uri.fromFile(new File(sdCard +
new String(new char[sdCard.replaceAll("[^/]", "").length()])
.replace("\0", "/..") + getFilesDir() + "/" + fileName));
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent , "Send email..."));
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully! path:" + fileLocation,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
例外:
接近()从未被明确要求在数据库“/data/data/com.google.android.gms/databases /phenotype.db' android.database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象
Erro r找到电子邮件提供商的版本.....
ctivity com.android.internal.app.ChooserActivity泄漏了最初在此注册的IntentReceiver [email protected]。你是否错过了对unregisterReceiver()的调用? android.app.IntentReceiverLeaked:Activity com.android.internal.app.ChooserActivity泄漏了最初在这里注册的IntentReceiver [email protected]。你是否错过了对unregisterReceiver()的调用?
我在做什么错?
的例子。根据我的代码,也可以发送电子邮件的问题是要附加文件。 –