title: Android 11 update broke my File writing code
body: <p>I've used this snippet before to <strong>write a simple text file</strong> with a name and a body:</p>
  <pre><code>public static File writeTextFile(String sFileName, String sBody) throws IOException {
          File root = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);
  
      if (!root.exists()) {
          logFile(&quot;Not yet created, creating right now: &quot; + root.getPath());
          root.mkdirs();
  
          logFile(&quot;Creation was successful? &quot; + (root.exists() ? &quot;Yes.&quot; : &quot;No.&quot;));
  
      } else {
          logFile(&quot;Path exists: &quot; + root.getPath());
      }
  
    
      File       measurementFile = new File(root, sFileName);
      FileWriter writer  = new FileWriter(measurementFile);
      writer.append(sBody);
      writer.flush();
      writer.close();
  
      return measurementFile;
  
  }
  </code></pre>
  <p>It is working well <strong>except for Android 11</strong> which I just realized today after testing it on a OnePlus N10 which runs Android 11.</p>
  <hr />
  <p>The problem is <strong>I don't get any described error message just a simple exception</strong> and I can't seem to find a way to go on after this problem has occurred.</p>
  <pre><code> java.io.FileNotFoundException: /storage/emulated/0/example_myapp/content_file_to_upload_1635424827184: open failed: ENOENT (No such file or directory)
       at libcore.io.IoBridge.open(IoBridge.java:492)
       at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:236)
       at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:186)
       at java.io.FileWriter.&lt;init&gt;(FileWriter.java:90)
       at com.myapp.example.util.FileUtil.writeTextFile(FileUtil.java:39)
       at com.myapp.example.services.ConnectionServiceForDownloadData$1.onCharacteristicChanged(ConnectionServiceForDownloadData.java:265)
       at android.bluetooth.BluetoothGatt$1$8.run(BluetoothGatt.java:478)
       at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:780)
       at android.bluetooth.BluetoothGatt.access$200(BluetoothGatt.java:41)
       at android.bluetooth.BluetoothGatt$1.onNotify(BluetoothGatt.java:472)
       at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:306)
       at android.os.Binder.execTransactInternal(Binder.java:1170)
       at android.os.Binder.execTransact(Binder.java:1134)
   Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
       at libcore.io.Linux.open(Native Method)
       at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
       at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
       at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
       at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7924)
       at libcore.io.IoBridge.open(IoBridge.java:478)
      ... 12 more
  </code></pre>
  <p>Any help is greatly appreciated.</p>