Gmail 5.0 应用程序失败并显示“附件的权限被拒绝";当它收到 ACTION_SEND 意图时 [英] Gmail 5.0 app fails with "Permission denied for the attachment" when it receives ACTION_SEND intent

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

问题描述

我的应用创建带有附件的邮件,并使用带有 Intent.ACTION_SEND 的 Intent 来启动邮件应用.

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".

logcat 上没有来自 Gmail 的有用邮件.我只在 Android KitKat 上测试了 Gmail 5.0,但在多个设备上.

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 的安全问题.

I'm aware of the security concerns with 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 为其从 Intent 接收的附件添加了一些安全检查.这些与 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.

当附件 Uri 是 file://时,它只会接受来自外部存储的文件、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 能够找到调用方应用程序,这仅在调用方要求结果时才可靠.在上面的代码中,您没有要求结果,因此 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.

或者更好的是,使用内容提供商.

Or better yet, use a content provider.

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

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