I know there are many other posts with this issue, but none seem to solve mine. I am trying to create a file on Enviroment.DIRECTORY_DOWNLOADS
, email that file, and then delete it. However, I am getting an error when I initialize the file output stream fos = new FileOutputStream(file);
.
The error I get is:
java.io.FileNotFoundException: /CheckListReport_2015_07_19.txt: open failed: ENOENT (No such file or directory)
In my manifest I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
outside of the applications tag
Below is the code:
String report = new SimpleDateFormat("yyyy_MM_dd").format(Calendar.getInstance().getTime());
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
String lineToWrite;
for (Map.Entry entry : CheckboxHandler.data.entrySet()) {
lineToWrite = entry.getKey() + ", " + entry.getValue() + "\n";
fos.write(lineToWrite.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
String subject = "CheckListReport_" + report + ".txt";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
file.delete();
use this permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Try this code to read files from Download
directory:
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
Change this:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + Environment.DIRECTORY_DOWNLOADS + "/CheckListReport_" + report + ".txt"));
to:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With