I'm unable to create directory in Android 10. It's working on devices till Android Oreo . I tried two ways for creating folders.
UsingFile f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin"); if (!f.isFile()) { if (!(f.isDirectory())) { success = f.mkdir(); } }
Using
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin"); if (!f.isFile()) { if (!(f.isDirectory())) { if (Build.VERSION.SDK_INT >= 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(); } } }
I've implemented the run-time permissions and
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)
are all set.
Answer:
As first revealed in March 2019, you can no longer access any external storage or removable storage by default on Android 10 Location. This includes Environment.getExternalStorageDirectory() and other methods on Environment (for example, getExternalStoragePublicDirectory()).
For Android 10 and 11, you can add android:requestLegacyExternalStorage="true" to theUse methods on the Context (such as getExternalFilesDir()) to access directories on external storage that your app can write to. You don't need any permissions to use these directories on Android 4.4. However, when your app is uninstalled, the data stored here will be deleted.
The above is the detailed content of Why Can't I Create a Directory in Android 10, Even with Necessary Permissions?. For more information, please follow other related articles on the PHP Chinese website!