gabboSonc
gabboSonc

Reputation: 351

Intent.ACTION_SEND doesn't send attachment

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[]{"[email protected]"});
        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.

Upvotes: 0

Views: 842

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

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.

Upvotes: 1

Related Questions