简体   繁体   中英

Android: Send e-mail via intent with file as attachment

There are many posts on this topic, but I can't find the solution for my problem...

Following: I would like to send a file out of my app via an e-mail attachment.
Sending the file via Whatsapp, save to Google Drive,... works, but not for K-9 Mail or Gmail ("Unable to attache file" Toast message is displayed).

Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType("application/zip");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/Download/ExportFile.zip"));
//intentShareFile.putExtra(Intent.EXTRA_TEXT, "message");
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareFile.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

startActivity(Intent.createChooser(intentShareFile, "Share File"));

I don't understand why it works for all apps, except e-mail apps.
Can anyone help me out?
Thanks in advance.

I think your issue is related to the path of the file you are using here (ie Uri.parse("/sdcard/Download/ExportFile.zip") )

There is a code example to send an email with an attachment on the Android Developers docs here . In the example they pass the Uri as follows: Uri.parse("content://path/to/email/attachment") . Notice this is called a ContentUri , read more about it form here .

Hope this turns useful for you!

Amr EIZawawy is right. I need to create the Uri of the file and use the File Provider API.
I don't know if I did too much, but this is my solution:

  1. Create a file called "file_paths.xml" in a directory "xml" (create first) within your "res" directory (sibling of layout directory).
    The file needs to contain the path to the file you want to share. For the external Download directory this is:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="download" path="Download/"/>
</paths>
// The internal path you'd be <files-path name.../> see [FileProvider][1] for all possibilities

  1. Define the File provider within the AndroidManifest.xml with a meta-data link to the just created XML file:
<application
    ...

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.<your-app-package-name>.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />   // Link to the above created file
    </provider>
    ...
  1. Implement the code:
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType("application/zip");
File fileDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File newFile = new File(fileDirectory, "ExportFile.zip");
String authority = "com.bennoss.myergometertrainer.fileprovider";   // Must be the same as in AndroidManifest.
Uri contentUri = FileProvider.getUriForFile(getContext(), authority, newFile);
intentShareFile.putExtra(Intent.EXTRA_STREAM, contentUri);
//intentShareFile.putExtra(Intent.EXTRA_TEXT, "xxx");
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(intentShareFile, "Share File"));

For more information see: FileProvider

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM