开发者

Android Email Intent Not Sending Attached File

开发者 https://www.devze.com 2023-01-14 03:03 出处:网络
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 atta

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(f开发者_StackOverflowileName));
//Use a chooser to decide whether email or mms
startActivity(Intent.createChooser(sendIntent, "Send email..."));

Any ideas?


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消