title: Fails to access the `Downloads` folder
body: <p>I am trying to save a txt file into the Downloads folder in my first abndroid app.</p>
  <pre><code>
  public class DisplaySettingsActivity extends AppCompatActivity implements View.OnClickListener {
  
      private H300sVoipSettings settings;
  
      Button saveIntoFile;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_display_settings);
  
          this.settings = (H300sVoipSettings) getIntent().getSerializableExtra(&quot;H300sVoipSettings&quot;);
  
          this.saveIntoFile = (Button)findViewById(R.id.save);
          this.saveIntoFile.setOnClickListener(this);
      }
  
  private String saveFile(){
          Log.d(&quot;Η300s&quot;,&quot;Saving&quot;);
          String state = Environment.getExternalStorageState();
          if (!Environment.MEDIA_MOUNTED.equals(state)) {
              Log.e(&quot;H300s&quot;,&quot;Unable to detect external storage&quot;);
              return null;
          }
  
          DateTimeFormatter pattern = DateTimeFormatter.ofPattern(&quot;yyyMMdd&quot;);
          File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
  
          file = new File( file.getAbsolutePath(),&quot;voip_h300s_&quot;+pattern.format(LocalDate.now())+&quot;.txt&quot;);
          Log.d(&quot;H300s&quot;,file.toString());
          try {
              Log.d(&quot;H300s&quot;,&quot;Saving&quot;);
              this.settings.save(file);
              Log.d(&quot;H300s&quot;,&quot;Saved&quot;);
              Log.d(&quot;H300s&quot;,file.getAbsolutePath());
              return file.getAbsolutePath();
          } catch (Exception e) {
              Log.e(&quot;H300s&quot;,e.toString());
              Log.e(&quot;H300s&quot;,Log.getStackTraceString(e));
              return null;
          }
      }
  }
  </code></pre>
  <p>But this piece of code in my function:</p>
  <pre><code> this.settings.save(file);
  </code></pre>
  <p>Fails to be saved due to lack of permissions. As the following log shows:</p>
  <pre><code>2021-04-24 14:48:18.498 8208-8208/com.example.vodafone_fu_h300s E/H300s: java.io.IOException: Permission denied
  2021-04-24 14:48:18.499 8208-8208/com.example.vodafone_fu_h300s E/H300s: java.io.IOException: Permission denied
          at java.io.UnixFileSystem.createFileExclusively0(Native Method)
          at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
          at java.io.File.createNewFile(File.java:1008)
          at pc_magas.vodafone_fu_h300s.screens.DisplaySettingsActivity.saveFile(DisplaySettingsActivity.java:97)
          at pc_magas.vodafone_fu_h300s.screens.DisplaySettingsActivity.onClick(DisplaySettingsActivity.java:120)
          at android.view.View.performClick(View.java:6597)
          at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
          at android.view.View.performClickInternal(View.java:6574)
          at android.view.View.access$3100(View.java:778)
          at android.view.View$PerformClick.run(View.java:25906)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:193)
          at android.app.ActivityThread.main(ActivityThread.java:6718)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  </code></pre>
  <p>What I want to do is to save a txt file into Downloads Folder and make it accessible to the user via file manager. The class <code>H300sVoipSettings</code> has the following:</p>
  <pre><code>public class H300sVoipSettings implements Serializable
  {
      private String username = null;
  
      private String password = null;
  
      private String primary_registar =  null;
  
      private String primary_registar_port = null;
  
      private String secondary_registar = null;
  
      private String secondary_registar_port = null;
  
      private String primary_proxy = null;
  
      private String primary_proxy_port = null;
  
      private String secondary_proxy = null;
  
      private String secondary_proxy_port = null;
  
      private String sip_domain = null;
  
      private String sip_number = null;
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getPrimary_registar() {
          return primary_registar;
      }
  
      public void setPrimary_registar(String primary_registar) {
          this.primary_registar = primary_registar;
      }
  
      public String getPrimary_registar_port() {
          return primary_registar_port;
      }
  
      public void setPrimary_registar_port(String primary_registar_port) {
          this.primary_registar_port = primary_registar_port;
      }
  
      public String getSecondary_registar() {
          if(secondary_registar == null || secondary_registar.trim().equals(&quot;&quot;)){
              return null;
          }
          return secondary_registar;
      }
  
      public void setSecondary_registar(String secondary_registar) {
          this.secondary_registar = secondary_registar;
      }
  
      public String getSecondary_registar_port() {
          return secondary_registar_port;
      }
  
      public void setSecondary_registar_port(String secondary_registar_port) {
          this.secondary_registar_port = secondary_registar_port;
      }
  
      public String getPrimary_proxy() {
          return primary_proxy;
      }
  
      public void setPrimary_proxy(String primary_proxy) {
          this.primary_proxy = primary_proxy;
      }
  
      public String getPrimary_proxy_port() {
          return primary_proxy_port;
      }
  
      public void setPrimary_proxy_port(String primary_proxy_port) {
          this.primary_proxy_port = primary_proxy_port;
      }
  
      public String getSecondary_proxy() {
          return secondary_proxy;
      }
  
      public void setSecondary_proxy(String secondary_proxy) {
          this.secondary_proxy = secondary_proxy;
      }
  
      public String getSecondary_proxy_port() {
          return secondary_proxy_port;
      }
  
      public void setSecondary_proxy_port(String secondary_proxy_port) {
          this.secondary_proxy_port = secondary_proxy_port;
      }
  
      public String getSip_domain() {
          return sip_domain;
      }
  
      public void setSip_domain(String sip_domain) {
          this.sip_domain = sip_domain;
      }
  
      public String getSip_number() {
          return sip_number;
      }
  
      public void setSip_number(String sip_number) {
          this.sip_number = sip_number;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public static H300sVoipSettings createFromJson(String jsonString) throws IllegalArgumentException, JSONException {
          if(jsonString == null || jsonString.trim().equals(&quot;&quot;)){
              throw new IllegalArgumentException(&quot;JsonString Should not be empty&quot;);
          }
  
          JSONArray settingsJson = new JSONArray(jsonString);
          H300sVoipSettings settings = new H300sVoipSettings();
  
          for (int i = 0; i &lt; settingsJson.length(); i++) {
              JSONObject item = settingsJson.getJSONObject(i);
              if(item.getString(&quot;type&quot;).equals(&quot;provider&quot;)){
                  settings.setPrimary_registar(item.getString(&quot;primary_registrar&quot;));
                  settings.setPrimary_registar_port(item.getString(&quot;primary_registrar_port&quot;));
                  settings.setPrimary_proxy(item.getString(&quot;primary_proxy&quot;));
                  settings.setPrimary_proxy_port(item.getString(&quot;primary_proxy_port&quot;));
                  settings.setSip_domain(item.getString(&quot;sip_domain&quot;));
                  String secondary_proxy = item.getString(&quot;secondary_proxy&quot;);
                  if(secondary_proxy != null &amp;&amp; !secondary_proxy.trim().equals(&quot;&quot;)){
                      settings.setSecondary_proxy(secondary_proxy.trim());
                  }
                  settings.setSecondary_proxy_port(item.getString(&quot;secondary_proxy_port&quot;));
                  settings.setSecondary_registar(item.getString(&quot;secondary_registrar&quot;));
                  settings.setSecondary_registar_port(item.getString(&quot;secondary_registrar_port&quot;));
              } else if(item.getString(&quot;type&quot;).equals(&quot;number&quot;)){
                  settings.setSip_number(item.getString(&quot;sip_number&quot;));
                  settings.setUsername(item.getString(&quot;username&quot;));
                  settings.setPassword(item.getString(&quot;password&quot;));
              }
          }
  
          return settings;
      }
  
      public boolean equals(H300sVoipSettings other){
  
          boolean truth =  other.getPassword().equals(this.getPassword()) &amp;&amp;
                 other.getUsername().equals(this.getUsername()) &amp;&amp;
                 other.getSip_number().equals(this.getSip_number()) &amp;&amp;
                 other.getSip_domain().equals(this.getSip_domain()) &amp;&amp;
                 other.getPrimary_proxy().equals(this.getPrimary_proxy()) &amp;&amp;
                 other.getPrimary_proxy_port().equals(this.getPrimary_proxy_port()) &amp;&amp;
                 other.getPrimary_registar().equals(this.getPrimary_registar()) &amp;&amp;
                 other.getPrimary_registar_port().equals(this.getPrimary_registar_port()) &amp;&amp;
                 other.getSecondary_proxy_port().equals(this.getSecondary_proxy_port()) &amp;&amp;
                 other.getSecondary_registar_port().equals(this.getSecondary_registar_port());
  
  
          truth = truth &amp;&amp; ((other.getSecondary_proxy() == null &amp;&amp; this.getSecondary_proxy() == null) || (other.getSecondary_proxy().equals(this.getSecondary_proxy())));
          truth = truth &amp;&amp;
                  (
                          (other.getSecondary_registar() == null &amp;&amp; this.getSecondary_registar() == null) ||
                                  (
                                          other.getSecondary_registar().equals(this.getSecondary_registar())
                                  )
                  );
          return truth;
      }
  
      public String toString()
      {
          StringBuilder txt = new StringBuilder();
  
              txt.append(&quot;Phone Number: &quot;);
              txt.append(this.getSip_number());
              txt.append(&quot;\n&quot;);
  
              txt.append(&quot;Username: &quot;);
              txt.append(this.getUsername());
              txt.append(&quot;\n&quot;);
  
              txt.append(&quot;Password: &quot;);
              txt.append(this.getPassword());
              txt.append(&quot;\n&quot;);
  
              txt.append(&quot;Sip Domain: &quot;);
              txt.append(this.getSip_domain());
              txt.append(&quot;\n&quot;);
  
              txt.append(&quot;Primary proxy: &quot;);
              txt.append(this.getPrimary_proxy());
              txt.append(&quot; Port: &quot;);
              txt.append(this.getPrimary_proxy_port());
              txt.append(&quot;\n&quot;);
  
              txt.append(&quot;Secondary proxy: &quot;);
              String secondary_proxy = this.getSecondary_proxy();
              secondary_proxy = (secondary_proxy == null || !secondary_proxy.trim().equals(&quot;&quot;))?&quot;N/A&quot;:secondary_proxy;
              txt.append(secondary_proxy);
              txt.append(&quot; Port: &quot;);
              String secondaryProxyPort = this.getSecondary_proxy_port();
              secondaryProxyPort=(secondaryProxyPort == null || !secondaryProxyPort.trim().equals(&quot;&quot;))?&quot;N/A&quot;:secondaryProxyPort;
              txt.append(secondaryProxyPort);
              txt.append(&quot;\n&quot;);
  
              txt.append(&quot;Primary Registar: &quot;);
              String primaryRegistar = this.getPrimary_registar();
              txt.append(primaryRegistar);
              txt.append(&quot; Port: &quot;);
              String primaryRegistarPort = this.getPrimary_registar_port();
              txt.append(primaryRegistarPort);
              txt.append(&quot;\n&quot;);
  
              txt.append(&quot;Secondary Registar: &quot;);
              String secondary_registar = this.getSecondary_registar();
              secondary_registar = (secondary_registar == null || !secondary_registar.trim().equals(&quot;&quot;))?&quot;N/A&quot;:secondary_registar;
              txt.append(secondary_registar);
              txt.append(&quot; Port: &quot;);
              String secondaryRegistarPort = this.getSecondary_registar();
              secondaryRegistarPort=(secondaryRegistarPort == null || !secondaryRegistarPort.trim().equals(&quot;&quot;))?&quot;N/A&quot;:secondaryRegistarPort;
              txt.append(secondaryRegistarPort);
              txt.append(&quot;\n&quot;);
  
          return txt.toString();
      }
  
      public void save(File file) throws IOException {
          PrintWriter out = new PrintWriter(new FileWriter(file));
          out.println(&quot;********&quot;);
          out.print(&quot;Exported Date: &quot;);
          out.println(new Date().toString());
          out.println(&quot;********&quot;);
          out.print(this.toString());
          out.close();
      }
  }
  </code></pre>
  <p>And is used for data serialization. The application has the following in Android Manifest:</p>
  <pre><code>    &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  </code></pre>
  <p>Furthermore I have looked upon these answers whith no result whatsoever:</p>
  <ul>
  <li><a href="https://stackoverflow.com/a/62748149/4706711">https://stackoverflow.com/a/62748149/4706711</a></li>
  </ul>
  <p>So do you have any idea how I can access the donwloads folder?</p>