Android 10 中无法创建目录的问题
I'm unable to create directory in Android 10. It's working on devices till Android Oreo. I tried two ways for creating folders.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin"); if (!f.isFile()) { if (!(f.isDirectory())) { success = f.mkdir(); } }
Here, the variable success is always false which means the directory isn't created.
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(); } } }
which causes this exception:
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)
I've implemented the run-time permissions and
`
are all set.
答案:
从 2019 年 3 月首次披露的那样,您不再能默认访问 Android 10 上外部存储或可移动存储中的任意位置。这包括 Environment.getExternalStorageDirectory() 和 Environment 上的其他方法(例如,getExternalStoragePublicDirectory())。
对于 Android 10 和 11,您可以将 android:requestLegacyExternalStorage="true" 添加到清单中的
否则,您的选择是:
以上是为什么即使具有必要的权限,我也无法在 Android 10 中创建目录?的详细内容。更多信息请关注PHP中文网其他相关文章!