Android的:不能附加文件在非Gmail客户端电子邮件 [英] Android: Can't Attach File to Email in Non-Gmail Client

查看:171
本文介绍了Android的:不能附加文件在非Gmail客户端电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的code,我可以附加文件从我的,没有问题的应用程序的电子邮件 - 如果我使用Gmail应用程序作为我的电子邮件客户端。但是,任何其他邮件客户端无视我发送给它的附件。

Using the code below, I can attach files to an email from my application with no problem - if I use the Gmail application as my email client. However, any other email client disregards the attachment I send to it.

下面是我的code:

public static void sendEmail(Context context, String toAddress, String subject, String body, String attachmentPath) throws Exception{
        try {
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", toAddress, null));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_SUBJECT, subject);
            intent.putExtra(Intent.EXTRA_TEXT, body);

            File file = new File(attachmentPath);
            Uri uri = Uri.fromFile(file);

            intent.putExtra(Intent.EXTRA_STREAM, uri);

            context.startActivity(intent);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
}

有谁知道如何建立一个意图以这样的方式,任何非Gmail电子邮件客户端将承认和接受附件?

Does anyone know how to set up an Intent in such a way that any non-Gmail email client will recognize and accept an attachment?

感谢您。

推荐答案

下面是一些code的作品。为什么它应该作出任何区别,我不知道,但它的作品。

Here's some code that works. Why it should make any difference, I don't know, but it works.

public static void sendEmail(Context context, String toAddress, String subject, String body, String attachmentPath, String attachmentMimeType) throws Exception{
        try {
            Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{toAddress});
                intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                intent.putExtra(Intent.EXTRA_TEXT, body);

                File file = new File(attachmentPath);
                Uri uri = Uri.fromFile(file);

                intent.putExtra(Intent.EXTRA_STREAM, uri);
                intent.setType(attachmentMimeType);

                context.startActivity(Intent.createChooser(intent, "Choose an email application..."));
        }
        catch(Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
}

注意使用ACTION_SEND代替ACTION_SENDTO的,以及在调用的setType(...)。它似乎工作,但它不会过滤掉非电子邮件应用程序,是psented作为选项发送给$ P $的列表。好够我现在 - 除非有人对如何使这项工作仍然下渗目标列表中的任何想法过于应用程式

Notice the use of ACTION_SEND instead of ACTION_SENDTO, as well as the call to setType(...). It seems to work, but it doesn't filter out the list of non-email apps that are presented as options to send to. Good enough for me for now - unless someone has any ideas on how to make this work and still filter down the list of target apps too.

感谢那些谁提供的建议。希望这将帮助别人了。

Thanks to those who offered suggestions. Hopefully this will help someone else, too.

这篇关于Android的:不能附加文件在非Gmail客户端电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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