Q:

Android send email with attachment error

I want to send email with an attachment from my app (attachment is csv file), but Gmail says, that attachment couldn't be added. Is mistake in converting text into csv or in Adding attachment to gmail?
My code:
public class EmailInput extends DialogFragment {

    View mainView;
    TextView email;
    Button submitSend;
    ArrayList<String> resultsEmail;
    ArrayList<String> valuesMail;
    TextView soucetVysledkuMail;
    String subject = "Kubírovací kalkulačka";
    MainActivity MA;
    String attachmentFile;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MA = (MainActivity) getActivity();
        mainView = inflater.inflate(R.layout.fragment_email_input, container, false);
        email = (TextView) mainView.findViewById(R.id.email);
        submitSend = (Button) mainView.findViewById(R.id.submitSend);
        resultsEmail = ((MainActivity) getActivity()).getVysledky();
        valuesMail = ((MainActivity) getActivity()).getValues();
        soucetVysledkuMail = ((MainActivity) getActivity()).getSoucetVysledku();

        getDialog().setTitle("Odeslat výsledky");

        submitSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String FILENAME = "email-attachment.csv";
                String string = "";

                try {
                    FileOutputStream fOut = ((MainActivity) getActivity()).getContext().openFileOutput(FILENAME, Context.MODE_PRIVATE);

                    for (int i = 0; i < resultsEmail.size(); i++) {
                        string += resultsEmail.get(i) + "," + valuesMail.get(i);
                    }
                    string += "Součet výsledků:," + MA.getsoucetVsechVysledkuMA();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                attachmentFile = "mail-attachment.csv";
                Uri URI = Uri.parse("file://" + attachmentFile);

                String content = results();
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email.getText().toString()});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                emailIntent.putExtra(Intent.EXTRA_TEXT, content);
                emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
                emailIntent.setType("text/plain");
                startActivity(Intent.createChooser(emailIntent, "Vyberte aplikaci:"));
            }
        });

        return mainView;
    }

    public String results() {
        String content = "";
        for (int i = 0; i < resultsEmail.size(); i++) {
            int cislovka = i+1;
            content += cislovka + ". " + resultsEmail.get(i) + " (" + valuesMail.get(i) + ") \n";
        }
        content += "Součet výsledků: " + MA.getsoucetVsechVysledkuMA();
        return content;
    }
}

A:

There is a problem with permissions. If you want to attach file to your mail, it has to be saved in a public folder like Documents.

