shaneburgess
shaneburgess

Reputation: 15902

Android Email Intent Not Sending Attached File

I have created an application that sends an email with a recording, When the intent is fired and email is chosen as the app to send the attachment, you can see that there is an attachment but the attachment is not delivered.

Intent sendIntent = new Intent(Intent.ACTION_SEND);
//Mime type of the attachment (or) u can use sendIntent.setType("*/*")
sendIntent.setType("audio/3gp");
//Subject for the message or Email
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "My Recording");
//Full Path to the attachment
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));
//Use a chooser to decide whether email or mms
startActivity(Intent.createChooser(sendIntent, "Send email..."));

Any ideas?

Upvotes: 6

Views: 5158

Answers (3)

Magali Sganga
Magali Sganga

Reputation: 51

UriAttachment does not longer works. You have to use FileProvider. But you have to work a little bit to make it work.

This video explains it perfectly https://www.youtube.com/watch?v=wYvV4m-N9oY&t=535s[Share Photos & Files Using FileProvider]1.

But, it didn´t work for me, I had to do something else. I had to replace res/xml/file_paths.xml with:

<?xml version="1.0" encoding="utf-8"?>
   <paths>
     <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

As it is described in an answer to this question: FileProvider - IllegalArgumentException: Failed to find configured root. You should use only the lines you need to make it work.

I was fighting a long time with this until I found a proper way to do this.

Upvotes: 0

Jadamec
Jadamec

Reputation: 923

Starting with API level 24, you cannot use "file://" URIs for passing files between packages. Instead, you should implement FileProvider and pass the file using it.

Uri fileUri = FileProvider.getUriForFile(context, "com.yourdomain.yourapp.fileprovider", file);

The good thing about FileProvides is that you don't need WRITE_EXTERNAL_STORAGE permission (for API level 21 and above).

The best description as at another StackOverflow answer or in that documentation.

Upvotes: 3

shaneburgess
shaneburgess

Reputation: 15902

I figured it out, you need to make sure that your uri has "file://" in front of it.

Upvotes: 11

Related Questions