无法在 Android 10 中创建目录 [英] Can't create directory in Android 10

查看:39
本文介绍了无法在 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" 添加到清单中的 元素.这将使您选择使用旧存储模型,并且您现有的外部存储代码将起作用.

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 as getExternalFilesDir(), 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_DOCUMENTACTION_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆