Android电子邮件意图未发送附加文件。

6
我创建了一个应用程序,可以发送带有录音的电子邮件。当意图被触发并选择电子邮件作为发送附件的应用程序时,您可以看到有一个附件,但是附件没有被送达。请检查您的代码和设置以确保附件正确发送。
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..."));

有什么想法吗?


我也遇到了同样的问题,即使在开头加上“file://”。你找到了其他解决方法吗? - skamlet
1
我找到了问题所在,我的文件是私有的,因此邮件应用程序无法读取该文件。现在它已经可以正常工作了。 - skamlet
@D4r7h 你是如何将文件“设为非私有”的?使用 'file.SetReadable(true);' 吗? 我也尝试将文件移动到~文件夹,但没有成功。我有一个文本文件可以正确发送。 你能给我一个提示吗? - marco.marinangeli
3个回答

11

我明白了,您需要确保URI前面有"file://"。


3

从API 24开始,您不能使用“file://” URI在包之间传递文件。相反,您应该实现FileProvider并使用它传递文件。

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

FileProvides的好处在于,对于API级别21及以上的设备,您不需要WRITE_EXTERNAL_STORAGE权限。

最好的描述可以在另一个StackOverflow答案或文档中找到。


0

UriAttachment不再起作用。您必须使用FileProvider。但是您需要做一些工作才能使其正常工作。

这个视频完美地解释了它 https://www.youtube.com/watch?v=wYvV4m-N9oY&t=535s[分享 使用FileProvider共享照片和文件]1

但是,对我来说它没有起作用,我不得不做其他的事情。我不得不用以下内容替换res/xml/file_paths.xml:

<?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>

正如在FileProvider - IllegalArgumentException: Failed to find configured root问题的答案中所描述的那样,您应该仅使用必要的行使其正常工作。

我花了很长时间才找到一个合适的方法来解决这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接