I try to send a text file as attachment. I use the following code
private void sendEmail(File attachment, String subject) {
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "schafkopf.frapfe@gmail.com" });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, getString(R.string.email_description)
if(Build.VERSION.SDK_INT>=24){
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
Uri apkURI = FileProvider.getUriForFile(
this.getContext(),"com.pfefra.schafkopf.provider", attachment);
i = i.setDataAndType(apkURI, "text/message");
try {
startActivity(Intent.createChooser(i, String.format(
getString(R.string.email_send_headline), subject)));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), getString(R.string.email_no_client),
Toast.LENGTH_SHORT).show();
}
}
Definition in manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.pfefra.schafkopf.provider"
android:enabled="true"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files-path" path="."/>
</paths>
I choose Gmail. But instead of a file attachment I see //com.pfefra.schafkopf.provider/files-path.Log1234.txt as an additional address