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

I know there are many other posts with this problem, but none seem to be solving my problem. I am trying to create a file on Enviroment.DIRECTORY_DOWNLOADS

, upload that file and delete it. However, I get an error while initializing the file's output stream fos = new FileOutputStream(file);

.

The error I am getting:

java.io.FileNotFoundException: /CheckListReport_2015_07_19.txt: open failed: ENOENT (No such file or directory)

In my manifest, I am <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

outside the app 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[]{"checklistreportdata@gmail.com"});
    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();

      

+3


source to share


1 answer


use this permission:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

      

Try this code to read files from directory Download

:

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"));

      

in

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));

      

+2


source







All Articles