k3davis
k3davis

Reputation: 1105

Stray file path added to email on intent using FileProvider attachment

I've got an intent that uses FileProvider to read from internal file storage for my app to send a file via email (or similar apps). The code works great everywhere apparently except for Gmail, which strangely adds a version of the provider path itself to the list of addressees of the email.

This is the code generating the intent:

public static Intent getSendIntent(Uri uri) {
    final Intent i = new Intent(Intent.ACTION_SEND);
    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    i.setDataAndType(uri, "message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
    i.putExtra(Intent.EXTRA_SUBJECT, "Export");
    i.putExtra(Intent.EXTRA_TEXT, "See the attached...");
    i.putExtra(Intent.EXTRA_STREAM, uri);
    return i;
}

This is the code to start the activity:

File file = new File(getFilesDir(), filename);
Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.myapplication.provider", file);
Intent i = Utils.getSendIntent(uri);

try {
    startActivity(Intent.createChooser(i, "Send file..."));
} catch (Exception e) {
    Log.d(TAG, "failed to start send activity: " + e.getMessage());
    Toast.makeText(MainActivity.this, "No suitable activity found for export.", Toast.LENGTH_SHORT).show();
}

Works well in Slack, Evernote, etc. but in Gmail in addition to the email address I've provided, another addressee in this kind of format is added:

//com.example.myapplication.provider/my_files/filefile.csv

which prevents the email from sending until it's manually removed from the message. Everything else about the message is as expected.

Any clue how to prevent this?

Upvotes: 1

Views: 549

Answers (1)

k3davis
k3davis

Reputation: 1105

The following Captain Obvious solution resolved the problem.

Replace:

i.setDataAndType(uri, "message/rfc822");

with:

i.setType("message/rfc822");

since, as @CommonsWare's question inferred, the file content isn't itself in rfc822 format.

Upvotes: 2

Related Questions