tags:

views:

47

answers:

1

I have an Android app that saves a text file directly onto the phone, in the app's install directory. I need to allow the user to create a new email, attaching this saved text file. When I start the intent to send the email, everything shows up in Gmail correctly, but the attachment does not get sent. All of my searches on stack overflow seem to only deal with attaching an image file from the SD card. Below is the code that I used. Please let me know if I have done something incorrectly.

File myFile = new File(getFilesDir() + "/" + "someFile.txt");
FileOutputStream stream = null;

if( file != null )
{
    steam = openFileOutput("someFile.txt", Context.MODE_WORLD_READABLE);
    stream.write(some_data);

    Uri uri = Uri.fromFile(myFile);

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    sendIntent.putExtra(Intent.EXTRA_TEXT, email_text);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);

    file.close();

    startActivity(Intent.createChooser(sendIntent, "Email:"));
}

I've also tried sendIntent.setType("application/octet-stream"); but that didn't make a difference. I'm at a loss for why the file doesn't attach and get sent.

Any ideas?

+2  A: 

I've seen this before and the only way I could solve it was by writing the file to the SD card.

It's worth trying writing to the file to the SD card and attaching it if only to eliminate the files location as the cause of the problem.

Al Sutton
hmmm... that's unfortunate. I'll give this a try and see if that works better. Thanks Al.What approach did you take if no SD card was present? Did you decide to just attach all of your file data in the EXTRA_TEXT portion of the Intent email?
Michael
If no card was present I show a "You need an SD card message".I know it's far from ideal, but it avoids the issue of users complaining that the attachment didn't arrive as an attachment and issues relating to them doing File -> Save in their mail client and complaining their other application couldn't import the file (due to mail headers being in the file).
Al Sutton
Thanks again Al. Changing my code to store the file on the SD card works.
Michael