Gmail的5.0应用程序失败,"权限被拒绝的附件和QUOT;当它接收ACTION_SEND意图 [英] Gmail 5.0 app fails with "Permission denied for the attachment" when it receives ACTION_SEND intent

查看:151
本文介绍了Gmail的5.0应用程序失败,"权限被拒绝的附件和QUOT;当它接收ACTION_SEND意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序创建邮件带有附件,并且使用意图与 Intent.ACTION_SEND 启动邮件应用程序。

My app creates mails with attachments, and uses an intent with Intent.ACTION_SEND to launch a mail app.

这适用于所有的邮件应用程序我与测试,除了新的Gmail 5.0,在此邮件打开,但不附件(这与Gmail 4.9作品),显示错误:权限被拒绝的附件

It works with all the mail apps I tested with, except for the new Gmail 5.0 (it works with Gmail 4.9), where the mail opens without attachment, showing the error: "Permission denied for the attachment".

有来自Gmail的logcat中没有有用的信息。我只测试的Gmail 5.0在Android奇巧,但在多个设备上。

There are no useful messages from Gmail on logcat. I only tested Gmail 5.0 on Android KitKat, but on multiple devices.

我创造附件这样的文件:

I create the file for the attachment like this:

String fileName = "file-name_something_like_this";
FileOutputStream output = context.openFileOutput(
        fileName, Context.MODE_WORLD_READABLE);

// Write data to output...

output.close();
File fileToSend = new File(context.getFilesDir(), fileName);

我所知道的有 MODE_WORLD_READABLE 的安全问题。

我发的意图是这样的:

public static void compose(
        Context context,
        String address,
        String subject,
        String body,
        File attachment) {

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("message/rfc822");
    emailIntent.putExtra(
            Intent.EXTRA_EMAIL, new String[] { address });
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, body);

    emailIntent.putExtra(
            Intent.EXTRA_STREAM,
            Uri.fromFile(attachment));

    Intent chooser = Intent.createChooser(
            emailIntent, 
            context.getString(R.string.send_mail_chooser));

    context.startActivity(chooser);
}

有没有什么创建文件或发送的意图时,我做错了什么?是否有更好的方式开始新的应用程序,邮件带附件?另外 - 有一个人遇到了这个问题,并找到了一个解决方法是

Is there anything I do wrong when creating the file or sending the intent? Is there a better way to start a mail app with attachment? Alternatively - has someone encountered this problem and found a workaround for it?

谢谢!

推荐答案

GMail的5.0增加了一些安全检查,以附件接收来自意图。这些无关的unix的权限,因此一个事实,即文件是可读的并不重要。

GMail 5.0 added some security checks to attachments it receives from an Intent. These are unrelated to unix permissions, so the fact that the file is readable doesn't matter.

在开放的附件是一个文件://,它会只接受来自调用应用程序的私有数据目录从外部存储时,Gmail本身的私有目录,或世界可读的文件,文件

When the attachment Uri is a file://, it'll only accept files from external storage, the private directory of gmail itself, or world-readable files from the private data directory of the calling app.

与此安全检查的问题是,它依赖于的Gmail能够找到呼叫者应用程序,这是唯一可靠的该呼叫者要求的结果。在您的code以上,你不问结果,因此Gmail不知道是谁的来电是,并拒绝您的文件。

The problem with this security check is that it relies on gmail being able to find the caller app, which is only reliable when the caller has asked for result. In your code above, you do not ask for result and therefore gmail does not know who the caller is, and rejects your file.

由于它在4.9为你工作,但不是在5.0,你知道这是不是一个UNIX权限问题,因此其原因必须是新的检查。

Since it worked for you in 4.9 but not in 5.0, you know it's not a unix permission problem, so the reason must be the new checks.

TL; DR的答案: 与startActivityForResult替换startActivity。

TL;DR answer: replace startActivity with startActivityForResult.

或者更好的是,使用一个内容提供商。

Or better yet, use a content provider.

这篇关于Gmail的5.0应用程序失败,"权限被拒绝的附件和QUOT;当它接收ACTION_SEND意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆