title: Writing a file to external storage android FileNotFoundException
body: <p>I want to write a file to external storage in android but get the FileNotFoundException. I have put the permission</p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt;
  </code></pre>
  
  <p>in my manifest and if I do a check like</p>
  
  <pre><code>Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
  </code></pre>
  
  <p>it returns true. I have tried different solutions from questions from SO (<a href="https://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage">Android saving file to external storage</a>; <a href="https://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android/8330635#8330635">Write a file in external storage in Android</a>; <a href="https://stackoverflow.com/questions/8931671/writing-to-external-storage-filenotfoundexception">Writing to external storage filenotfoundexception</a>) but none seemed to work for me. Sorry for posting a duplicate question but Ive been searching for days and couldnt find an answer.</p>
  
  <p>My Code is the following:</p>
  
  <pre><code>private void Writing(String erg) {
  
          TextView one = (TextView) findViewById(R.id.e1);
  
          File root = android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
  
          File dir = new File (root.getAbsolutePath());
          dir.mkdirs();
          File file = new File(dir, "Test.txt");
  
          try {
              FileOutputStream f = new FileOutputStream(file);
              PrintWriter pw = new PrintWriter(f);
              pw.println(erg);
              pw.flush();
              pw.close();
              f.close();
              one.setText("File created");
          } catch (FileNotFoundException e) {
              e.printStackTrace();
              one.setText("Error1");
          } catch (IOException e) {
              e.printStackTrace();
              one.setText("Error2");
          }
  
      }
  </code></pre>
  
  <p>The Textview one ends up with printing <strong>Error1</strong>.</p>
  
  <p>I'll appreciate any help.</p>