【发布时间】:2021-07-18 05:04:30
【问题描述】:
我在网上看了很多代码,但似乎都遇到了问题。
使用以下函数创建并保存文件:
private static String filename = "eulerY.txt" ;
private void saveData() {
FileOutputStream fos_FILE_eulerY = null;
String message = "hello";
try {
fos_FILE_eulerY = openFileOutput(filename , MODE_PRIVATE);
fos_FILE_eulerY.write(message.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos_FILE_eulerY != null) {
try {
fos_FILE_eulerY.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// export data
sendEmail ();
}
但是,当运行以下代码发送文件时,我一直遇到问题 ClipData.Item.getUri
正如使用此链接“https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi”的所有答案所建议的那样,打开Gmail时,它说“无法附加文件”
private void sendEmail (){
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
}
如果有任何方法可以发送此文件,我将不胜感激。
【问题讨论】:
标签: java android email email-attachments