我嘗試將文件附加到電子郵件。 用戶寫入文件。 完成後,文件保存在內部存儲器上,應通過電子郵件發送。如何將文件附加到電子郵件? - 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()的調用?
我在做什麼錯?
的例子。根據我的代碼,也可以發送電子郵件的問題是要附加文件。 –