Reputation: 2313
I'm trying to attach a text file to an email and I'm getting a weird error that I hope someone can help me with. It works fine when the user selects the gmail app from the chooser, but if they select the built in mail application, they see a toast that says "Unable to attach file".
The code looks like this:
public static void sendMail(Context context, String emailBody, String emailSubject, String emailAddress, String attachmentFilename){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { emailAddress});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
if(attachmentFilename != null) {
//Add the attachment by specifying a reference to our custom ContentProvider and the specific file of interest
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + Settings.VYPR_LOG_PROVIDER_AUTHORITY + "/" + attachmentFilename));
}
context.startActivity(emailIntent);
}
Anyone have any thoughts on what might be going on here? Most of what I have seen on here has to do with the attachment being on the SD card. I actually didn't write this code myself, but it seems like that must not be the issue here since it does work if the user selects the gmail app rather than the built in one.
Thanks in advance!
Upvotes: 1
Views: 1476
Reputation: 400
I experienced the same problem long time ago. Had to make the use of the Gmail app mandatory to send attachments. I could not figure out why the built-in email app did not work.
If you are trying to receive the attachment to a specific email address, you may also consider deploying a web service to upload the attachment.
Hope it helps.
Upvotes: 1