无法在Android 10中创建目录 [英] Can't create directory in Android 10
问题描述
我无法在android 10中创建目录.直到android Oreo为止,该设备都可以在设备上使用.
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.mkdir()
:
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin");
if (!f.isFile()) {
if (!(f.isDirectory())) {
success = f.mkdir();
}
此处,变量success
始终为 false ,这意味着未创建目录.
Here, the variable success
is always false which means the directory isn't created.
使用 Files.createDirectory()
:
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();
}
}
这会导致此异常:
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
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
都准备好了.
推荐答案
正如2019年3月首次披露的那样,默认情况下,您不再有权访问可移动存储设备在Android 10及更高版本上.这包括Environment.getExternalStorageDirectory()
和Environment
上的其他方法(例如getExternalStoragePublicDirectory()
.
As was first disclosed back in March 2019, you no longer have access by default to arbitrary locations on external storage or removable storage on Android 10+. This includes Environment.getExternalStorageDirectory()
and other methods on Environment
(e.g., getExternalStoragePublicDirectory()
.
对于Android 10和11,您可以将android:requestLegacyExternalStorage="true"
添加到清单中的<application>
元素中.这使您可以选择旧式存储模型,并且您现有的外部存储代码将起作用.
For Android 10 and 11, you can add android:requestLegacyExternalStorage="true"
to your <application>
element in the manifest. This opts you into the legacy storage model, and your existing external storage code will work.
否则,您的选择是:
-
使用
Context
上的方法(例如getExternalFilesDir()
)访问应用可以写入的外部存储上的目录.您不需要任何权限即可在Android 4.4+上使用这些目录.但是,卸载应用程序后,存储在其中的数据将被删除.
Use methods on
Context
, such asgetExternalFilesDir()
, to get at directories on external storage into which your app can write. You do not need any permissions to use those directories on Android 4.4+. However, the data that you store there gets removed when your app is uninstalled.
使用存储访问框架,例如ACTION_OPEN_DOCUMENT
和ACTION_CREATE_DOCUMENT
.
Use the Storage Access Framework, such as ACTION_OPEN_DOCUMENT
and ACTION_CREATE_DOCUMENT
.
如果您的内容是媒体,则可以使用MediaStore
将媒体放置在标准媒体位置.
If your content is media, you can use MediaStore
to place the media in standard media locations.
这篇关于无法在Android 10中创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!