0

I am trying to save a string into a new file which is named after the date in the database. I have also given the read and write permission.

Here is the method that handles creating a file and writing on it.

  public void saveDocumentOnSD(Context context, String sFileName, String reportDetail) {
            try {
                if (StorageHelper.isExternalStorageReadable() && StorageHelper.isExternalStorageWritable()) {
                  //  File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
                    File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "");
                    if(!root.exists()) {
                        root.mkdirs();
                    }
                    File file = new File(root, sFileName);

                    FileWriter writer = new FileWriter(file);
                    writer.append(reportDetail+ "\n");
                    writer.flush();
                    writer.close();
                    popUpToast("Saved");
                }
                else{
                    popUpToast("No external storage"); // shows toast
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

I keep getting this error: java.io.FileNotFoundException: /storage/emulated/0/Download/Nov 24, 2017 (Permission denied) This happens on this line "FileWriter writer = new FileWriter(file);"

I have tried to change the file location to

 File root = new File(/mnt/media_rw/sdcard1);

It still gives the same error

java.io.FileNotFoundException: /mnt/media_rw/sdcard1/Nov 24, 2017 (Permission denied)

Am I doing something wrong?

Here is my Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coursework.madsleuth">

    <uses-feature android:name="android.hardware.camera2"
        android:required="true"/>

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

    <application
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sushan Limbu
  • 43
  • 1
  • 8

0 Answers0