Skip to content Skip to sidebar Skip to footer

Android: Can't Attach File To Email In Non-gmail Client

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 disreg

Solution 1:

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

publicstaticvoid 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, newString[]{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.

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

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

Post a Comment for "Android: Can't Attach File To Email In Non-gmail Client"