0

I am trying to create file in internal storage and then send it via email in Android. However, I still get file not found or similar errors. Please, help!

String FILENAME = "TestFile.txt";

Sending file by button click

    btnSendFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File gpxfile = getFile();
            Uri path = Uri.fromFile(gpxfile);
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            Context context = v.getContext();
            String email = "MYEMAILHERE";
            i.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
            i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            i.putExtra(Intent.EXTRA_TEXT, "Text");
            i.putExtra(Intent.EXTRA_STREAM, path);
            context.startActivity(Intent.createChooser(i, context.getString("Sending...")));
        }
    });
}

Creating file in internal storage

private void createTestFile() {
    try {
        FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
        fos.write("Your content".getBytes());
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Get file function. Return File if file exists

private File getFile() {
    return new File(getFilesDir() + "/" + FILENAME);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Anastasia
  • 26
  • 1
  • 7

1 Answers1

0

Maybe you need to make

Context.openFileOutput(FILENAME, Context.MODE_APPEND);

Instead of that one you used? I think someone had similiar problem over here: android what is wrong with openFileOutput?

BigRedChick
  • 79
  • 1
  • 7