I am developing an application that sends processing reports via email. When completing the processing data in the application, the user sends an email containing the processing data. However, I am facing a problem on Android 11. When attaching the report to the email, regardless of the email application, a message appears: the file could not be attached. I've been researching a little and saw that it may be related to the permission to access the internal storage of the device on devices with version of android 11. I would like me to help you send email with file attached on android 11.
My code:
Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true"
tools:ignore="AllowBackup"
tools:targetApi="n">
<activity.....
<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>
</application>
My xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
Save file .pdf
val mDocProc = Document()
val file =
File(
Environment.getExternalStorageDirectory()
.toString() + "/" + "Relatório diário" + date + ".pdf"
)
if (file.exists()) {
file.delete()
}
//pdf file path
mFilePathDaily =
Environment.getExternalStorageDirectory()
.toString() + "/" + "Relatório diário " + date + ".pdf"
Class send email:
val report = File(mFilePathDaily)
val uri = FileProvider.getUriForFile(
activity.requireContext(),
activity.requireContext().applicationContext.packageName + ".provider",
report
)
val i = Intent(Intent.ACTION_SENDTO)
i.data = Uri.parse("mailto:")
i.putExtra(Intent.EXTRA_EMAIL, arrayOf("testesdissertacao@gmail.com"))
i.putExtra(Intent.EXTRA_SUBJECT, EMAIL_SUBJECT)
i.putExtra(Intent.EXTRA_TEXT, "Segue em anexo o relatório de Benchmark")
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
i.putExtra(Intent.EXTRA_STREAM, uri)
//i.type = "image/png"
activity.requireContext().startActivity(Intent.createChooser(i, null))
progressDialog.dismiss()
Now the problem is that you can't find the report to attach to the email. I noticed that the report was saved inside that other folder in the emulator, and not in the root folder. I think that is why you are not finding the file to attach to the email.
Do not attach the file to the email
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…