Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open failed: ENOENT (No such file or directory) even with permissions

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();
like image 765
Brejuro Avatar asked Oct 31 '22 23:10

Brejuro


1 Answers

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()));
like image 200
Anand Singh Avatar answered Nov 15 '22 06:11

Anand Singh