androidandroid-intenturiandroid-fileprovider

Attaching cache file to GMail through FileProvider and Intent not working


So I've been banging my head against the wall for the past day trying to figure out why a file won't attach to an email. Every time the app runs, I get a little toast message that pops up saying "Couldn't Attach File". The To and Subject fields fill just as I expect. First question is, how can I find out further information behind this error? This message is thrown from the GMail app and not my own program. It would certainly point me in the correct direction so I can debug further on my own if I had a reason for this error. I have included info below that may be relevant. The file size is 78.5kb and I can verify that the file exists and has the correct content I'd like to attach. Permissions on the file are rw for owner and group according to the file explorer.

Something interesting that I discovered while typing up this post; when using the file explorer for adding an attachment in the gmail app, the Android/data directory is not an option! It shows up when you are using the file explorer outside of the gmail app though. So I'm thinking this may be a permissions problem then? It cannot access this folder. If that's the case, what would be a recommended location to store this file? Ideally it would be some sort of cache location or temporary file location in this instance.

I have tried adding attachments in the Android Outlook app instead of GMail and the file does get attached.

Trying to run on Android 11 API 30 using the Pixel 4 Emulator.

Code for email intent:

protected void sendEmail(File f){
    final String[] TO = { "foo@bar.com" };

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, f.getName().replaceAll("(?i).pdf", ""));

    if (!f.exists() || !f.canRead()) {
        Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Uri uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID, f);
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

    try {
        startActivity(emailIntent);
        finish();
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MainActivity.this,
                "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }

AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-cache-path name="external_cache" path="."/>
</paths>

Value of f:

/storage/emulated/0/Android/data/com.bar.foo/cache/Form2020-12-27.pdf

Value of uri path:

content://com.bar.foo/external_cache/Form2020-12-27.pdf

Solution

  • Use ACTION_SEND instead.

    The TOwill come true here too.

    Add:

    emailIntent.setType("message/rfc822");
    

    You can remove the setData() call;