-1

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

frapfe
  • 1
  • 1

1 Answers1

0

ACTION_SEND does not use a Uri in the "data" facet. Also, there is no official MIME type named text/message.

Replace:

i = i.setDataAndType(apkURI, "text/message");

with:

i = i.setType(...).putExtra(Intent.EXTRA_STREAM, apkURI);

where you replace the ... with whatever the real MIME type is for whatever this attachment is.

Also, get rid of if(Build.VERSION.SDK_INT>=24){ — always set FLAG_GRANT_READ_URI_PERMISSION.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • i = i.setType(...).putExtra(Intent.EXTRA_STREAM, apkURI); works only sometimes! No idea what are the preconditions! – frapfe Mar 06 '18 at 20:08
  • @CommonsWare. `with whatever the real MIME type is for whatever this attachment is.` I wonder if an attachment determines the mime type. You can add five files all with different mime types. I think the mimetype indicates the type of mail to be send like "text/plain", "text/html". I always use "message/rfc822". – greenapps Mar 06 '18 at 21:06
  • @greenapps: [The documentation for `ACTION_SEND`](https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND) states that the MIME type "is the MIME type of the data being sent. get*Extra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the MIME type of the data in EXTRA_STREAM". Now, the OP's code may fail because that code is trying to use both extras; that is not documented and may not work with all `ACTION_SEND` implementations. – CommonsWare Mar 06 '18 at 21:22
  • Removing the putExtra(Intent.EXTRA_TEXT,--- ) has no impact – frapfe Mar 07 '18 at 09:51