Java 如何通过电子邮件发送文本文件作为附件-Android?
我在网上看到了许多代码,但它们似乎都遇到了问题 使用以下功能创建并保存文件:Java 如何通过电子邮件发送文本文件作为附件-Android?,java,android,email,email-attachments,Java,Android,Email,Email Attachments,我在网上看到了许多代码,但它们似乎都遇到了问题 使用以下功能创建并保存文件: private static String filename = "eulerY.txt" ; private void saveData() { FileOutputStream fos_FILE_eulerY = null; String message = "hello"; try {
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时,会显示“无法附加文件”
如果有任何方法发送此文件,我将不胜感激。如果您的
targetSdkVersion>=24
,则我们必须使用FileProvider
类来访问特定的文件或文件夹,以便其他应用程序可以访问这些文件或文件夹
第1步:
在AndroidManifest.xml
文件中添加以下代码
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
步骤3
现在,您可以将文件保存在您的包.private
文件夹中,并将保存在该文件夹中的文件uri
共享给其他应用程序,例如gmail
应用程序作为附件。现在,您的方法如下所示:
private void saveData() {
String filename = "eulerY.txt" ;
//FileOutputStream fos_FILE_eulerY = null;
File externalFilesDirectory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
File textFile = new File(externalFilesDirectory,filename);
String message = "hello";
try {
//fos_FILE_eulerY = openFileOutput(textFile.getAbsolutePath() , MODE_PRIVATE);
//fos_FILE_eulerY.write(message.getBytes());
FileWriter writer = new FileWriter(textFile);
writer.append(message);
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e){
e.getLocalizedMessage();
}
// export data
sendEmail (textFile);
}
private void sendEmail (File file){
//File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
//Uri path = Uri.fromFile(filelocation);
//FileProvider.getUriForFile(it, "${it.packageName}.provider", file)
Uri fileUri = FileProvider.getUriForFile(this,getPackageName()+".provider",file);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 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, fileUri);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
}
非常感谢你。我对你感激不尽:)
<paths>
<external-path name="external_files" path="."/>
</paths>
private void saveData() {
String filename = "eulerY.txt" ;
//FileOutputStream fos_FILE_eulerY = null;
File externalFilesDirectory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
File textFile = new File(externalFilesDirectory,filename);
String message = "hello";
try {
//fos_FILE_eulerY = openFileOutput(textFile.getAbsolutePath() , MODE_PRIVATE);
//fos_FILE_eulerY.write(message.getBytes());
FileWriter writer = new FileWriter(textFile);
writer.append(message);
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e){
e.getLocalizedMessage();
}
// export data
sendEmail (textFile);
}
private void sendEmail (File file){
//File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
//Uri path = Uri.fromFile(filelocation);
//FileProvider.getUriForFile(it, "${it.packageName}.provider", file)
Uri fileUri = FileProvider.getUriForFile(this,getPackageName()+".provider",file);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 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, fileUri);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
}