Can't create directory in Android 10 : <p>I'm unable to create directory in android 10. It's working on devices till android Oreo. </p>

<p>I tried two ways for creating folders.</p>

<p><strong>Using</strong> <code>File.mkdir()</code>:</p>

<pre><code>   File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin");
                    if (!f.isFile()) {
                        if (!(f.isDirectory())) {
                               success =  f.mkdir();
                        }
</code></pre>

<p>Here, the variable <code>success</code>  is always <strong>false</strong> which means the directory isn't created.</p>

<p><strong>Using</strong> <code>Files.createDirectory()</code>:</p>

<pre><code>   File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin");
                    if (!f.isFile()) {
                        if (!(f.isDirectory())) {
                            if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
                                try {
                                    Files.createDirectory(Paths.get(f.getAbsolutePath()));
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    Toast.makeText(getApplicationContext(), R.string.unable_to_download, Toast.LENGTH_LONG).show();
                                }
                            } else {
                                f.mkdir();
                            }
                        }
</code></pre>

<p>which causes this exception:</p>

<pre><code>pzy64.pastebinpro W/System.err: java.nio.file.AccessDeniedException: /storage/emulated/0/Pastebin
pzy64.pastebinpro W/System.err:     at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:391)
pzy64.pastebinpro W/System.err:     at java.nio.file.Files.createDirectory(Files.java:674)
</code></pre>

<p>I've implemented the run-time permissions and </p>

<pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
&lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
</code></pre>

<p>are all set.</p>
