Android: Can't Attach File to Email in Non-Gmail Client

最后都变了- 提交于 2020-01-07 05:11:07

问题


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.

Here is my 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;
        }
}

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?

Thank you.


回答1:


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;
        }
}

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.




回答2:


Do you have a specific email client in mind? A number of them don't even handle attachments in an intent or expect the intent to be populated differently.




回答3:


Generally this is only possible if the app vendor provides a specification about parameters you can pass in or you look into the source code. You need first to find out which activity/intent to use for the email client and afterwards, which extra's are processed in that activity.

If you are lucky you might find a list like this http://developer.android.com/guide/appendix/g-app-intents.html or that one Where is a list of available intents in Android? or the author of the app helps you to implement the right intent invocation.



来源:https://stackoverflow.com/questions/15557651/android-cant-attach-file-to-email-in-non-gmail-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!