Android Email Intent Not Sending Attached File
Asked Answered
C

3

6

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?

Collier answered 29/8, 2010 at 17:5 Comment(3)
I've got the same problem whith an image, even with the "file://" at the beginning. Heve you found another way to make it work?Burnejones
I found the issue, my file was private, so the mail application couldn't read the file. It now works fineBurnejones
@D4r7h How did you make your file "not private"? with 'file.SetReadable (true);'?? I also tried to move the file to ~ folder, with no luck. I have a txt file that gets correctly sent. Can you give me a hint, pls?Vermeil
C
11

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

Collier answered 29/8, 2010 at 20:43 Comment(0)
I
3

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.

Interrex answered 8/2, 2017 at 20:15 Comment(0)
W
0

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.

Wearproof answered 1/2, 2022 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.