0

my app sends a email when i press a button. I need to attach a .csv file. Here's the code:

Intent email = new Intent(Intent.ACTION_SEND);



        File file = new File(Environment.getExternalStorageState()+"/storage/sdcard0/myfile.csv");
        Uri path = Uri.fromFile(file);

        email.putExtra(Intent.EXTRA_EMAIL, new String[]{"aaa@xxx.it"});
        email.putExtra(Intent.EXTRA_SUBJECT, "Some text");
        email.putExtra(Intent.EXTRA_TEXT, "Some text");


       email.putExtra(Intent.EXTRA_STREAM, path);
        email.setType("application/octet-stream");
        startActivityForResult(Intent.createChooser(email, "Select client"),1222);

When I run the app, and I press the Send-button, a popup comes out and I choose the client email. When the client is opened, I can read the text, the subject, the email and I can see on the bottom the attachment (the .csv file). But when I send the Email, the receiver doesn't have any attachment.

gabboSonc
  • 351
  • 2
  • 4
  • 16
  • what do you expect `Environment.getExternalStorageState()` to do ? – njzk2 Jan 16 '15 at 15:17
  • surfing on internet I've found that you can't directly attach a file to a email for some security reasons – gabboSonc Jan 16 '15 at 15:19
  • possible duplicate of [How to send an email with a file attachment in Android](http://stackoverflow.com/questions/9974987/how-to-send-an-email-with-a-file-attachment-in-android) – AADProgramming Jan 16 '15 at 15:29

1 Answers1

1

Environment.getExternalStorageState() static method return state of primary "external" Storage like MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED instead of path of storage.

Remove getExternalStorageState which you are adding before file path:

File file = new File("/storage/sdcard0/myfile.csv");

and if file is stored in primary storage of device then use Environment.getExternalStorageDirectory to get storage directory instead of using static.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213