title: java.io.FileNotFoundException open failed: EEXIST (File exists) Android 11
body: <p>I was trying to download an image from a server and save it in the external memory, but in Android 11 it gives me an error when I try to create the file.
  I have granted permission to access the external storage.</p>
  <p>i searched a bit on the internet and they suggested me to put this code in the manifest, but it didn't work for android 11</p>
  <pre><code>android:requestLegacyExternalStorage=&quot;true&quot;
  </code></pre>
  <p>manifest</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:requestLegacyExternalStorage=&quot;true&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.TestDwonloadImgApp&quot;
          android:usesCleartextTraffic=&quot;true&quot;&gt;
          &lt;activity android:name=&quot;.MainActivity2&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  </code></pre>
  <p>MainActivity</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ImageView img = findViewById(R.id.img);
  
          ImmagineInterface ii = RetrofitManager.retrofit.create(ImmagineInterface.class);
          Call&lt;ResponseBody&gt; call = ii.downloadFile(&quot;/immaginimusei/arte-scienza.jpg&quot;);
          call.enqueue(new Callback&lt;ResponseBody&gt;() {
              @Override
              public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {
                  if (response.code() == 200) {
                      boolean result = writeResponseBody(response.body(), &quot;/immaginimusei/arte-scienza.jpg&quot;);
                      if(result) {
                          Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + &quot;/ArtHunter/immaginimusei/arte-scienza.jpg&quot;);
                          img.setImageBitmap(bitmap);
                      }
                  }
              }
  
              @Override
              public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {
                  Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + &quot;/ArtHunter/immaginimusei/arte-scienza.jpg&quot;);
                  img.setImageBitmap(bitmap);
              }
          });
      }
  }
  </code></pre>
  <p>writeResponseBody</p>
  <pre><code>public static boolean writeResponseBody(ResponseBody body, String dir1) {
          try {
              String state = Environment.getExternalStorageState();
              if (Environment.MEDIA_MOUNTED.equals(state)) {
                  // todo change the file location/name according to your needs
  
                  String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + &quot;/ArtHunter&quot;;
  
                  String path1 = path + dir1;
                  File f = new File(path1);
                  String path2 = f.getPath();
                  String nome = f.getName();
                  path2 = path2.replaceAll(&quot;/&quot; + nome, &quot;&quot;);
  
                  File directory = new File(path2);
                  if (!directory.exists())
                      directory.mkdirs();
  
                  File img = new File(path2, nome);
                  if (img.exists())
                      return true;
                  img.createNewFile();
  
                  InputStream inputStream = null;
                  FileOutputStream outputStream = null;
  
                  try {
                      byte[] fileReader = new byte[4096];
  
                      inputStream = body.byteStream();
                      outputStream = new FileOutputStream(img); //error here!
  
                      while (true) {
                          int read = inputStream.read(fileReader);
  
                          if (read == -1) {
                              break;
                          }
  
                          outputStream.write(fileReader, 0, read);
  
                      }
  
                      outputStream.flush();
  
                      return true;
                  } catch (IOException e) {
                      e.printStackTrace();
                      return false;
                  } finally {
                      if (inputStream != null) {
                          inputStream.close();
                      }
  
                      if (outputStream != null) {
                          outputStream.close();
                      }
                  }
              }
              return false;
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          }
      }
  </code></pre>
  <p>error</p>
  <pre><code>/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/ArtHunter/immaginimusei/arte-scienza.jpg: open failed: EEXIST (File exists)
  W/System.err:     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 com.theapplegeek.testdwonloadimgapp.MainActivity.writeResponseBody(MainActivity.java:93)
          at com.theapplegeek.testdwonloadimgapp.MainActivity$1.onResponse(MainActivity.java:47)
          at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
          at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$hVGjmafRi6VitDIrPNdoFizVAdk.run(Unknown Source:6)
          at android.os.Handler.handleCallback(Handler.java:938)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:245)
          at android.app.ActivityThread.main(ActivityThread.java:8004)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
      Caused by: android.system.ErrnoException: open failed: EEXIST (File exists)
          at libcore.io.Linux.open(Native Method)
          at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
          at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
  W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
          at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7865)
          at libcore.io.IoBridge.open(IoBridge.java:478)
          ... 13 more
  </code></pre>