con*_*ged 3 java email android email-attachments
我在网上查看了很多代码,但似乎都遇到了问题。
使用以下函数创建并保存文件:
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 ();
}
Run Code Online (Sandbox Code Playgroud)
但是,当运行下面的代码发送文件时,我不断遇到问题 ClipData.Item.getUri
并按照建议使用此链接“/sf/ask/3368225801/”中的所有答案-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..."));
}
Run Code Online (Sandbox Code Playgroud)
如果有任何方法可以发送此文件,我将不胜感激。
如果您的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>
Run Code Online (Sandbox Code Playgroud)
第2步
xml
在文件夹内创建一个文件夹res
。并创建一个文件,file_paths.xml
因为看上面的代码里面<meta-data>
。
文件路径.xml
<paths>
<external-path name="external_files" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
步骤 3
现在,您可以将文件保存在package.private
文件夹中,并且可以file uri
将此文件夹中保存的文件共享给其他应用程序,例如将gmail
应用程序作为attachment
. 现在你的方法看起来像:
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..."));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2387 次 |
最近记录: |