1

I'm creating an app that has an "export" feature which converts the user's data into a CSV file, and allows the user to send it as an attachment to somebody (presumably themselves).

The CSV file is created successfully, but when I try to send the email, I encounter a problem. The device looks like it is going to send the email with the appropriate attachment, but when the email is received... there is no attachment at all...

Here is the code I am using to send the email:

final Intent email = new Intent(android.content.Intent.ACTION_SEND);

  email.setType("text/html");
  email.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
  email.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(getString(R.string.email_1)));

  email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + getString(R.string.csv_title)));


  startActivity(Intent.createChooser(email, "Send mail..."));
dfetter88
  • 5,381
  • 13
  • 44
  • 55
  • possible duplicate of [problem sending an email with an attachment programmatically](http://stackoverflow.com/questions/1247983/problem-sending-an-email-with-an-attachment-programmatically) – Cheryl Simon Nov 10 '10 at 22:49
  • How did you create the CSV file? – rasen58 Apr 05 '13 at 00:20

3 Answers3

2
i've done for send any file from SD card with mail attachment..

Intent sendEmail= new Intent(Intent.ACTION_SEND);
       sendEmail.setType("rar/image");
       sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new        
         File("/mnt/sdcard/download/abc.rar")));
         startActivity(Intent.createChooser(sendEmail, "Email:"));
pradip
  • 185
  • 1
  • 3
  • 11
0

need the correct path for your file, if on SD card then...

sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse 
("file://"+Environment.getExternalStorageDirectory()+getString(R.string.csv_title)"));

Look here for addition information on setting the appropriate file path

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • What if it's not on the sd card? I'm thinking it's unwise to just assume that the user has an sd card... because if they don't, then the export feature would be essentially useless to them. – dfetter88 Nov 10 '10 at 21:41
  • set the path appropriately for wherever you are saving the file... The point I was making is that "file://" is incorrect, you need to specify the correct path to get the code to work – Aaron Saunders Nov 10 '10 at 21:46
0

GMail app accepts file:// Uris only if they are on the sdcard... and on an android 1.6 device I had even an issue with it accepting only file://sdcard/* Uris whereas the real external storage of this specific device is on another path.

Anyway, I have a real better behavior with attachments since I provide them through a ContentProvider.

Kevin Gaudin
  • 9,927
  • 3
  • 32
  • 34
  • Are you implying that by using a ContentProvider, I can avoid using the sdcard to store my CSV file? – dfetter88 Nov 11 '10 at 01:42
  • Yes, your ContentProvider implementation will provide an OutputStream from a file which can be in your application private directories. – Kevin Gaudin Nov 11 '10 at 13:57