简体   繁体   English

Android mkdirs() 使用 Environment.getExternalStorageDirectory() 在 Android 11 上返回 false

[英]Android mkdirs() return false on Android 11 with Environment.getExternalStorageDirectory()

Mkdirs() function is not working on Android 11. every thing is working fine on Android 10 and lower. Mkdirs() function 在 Android 11 上不起作用。在 Android 10 及更低版本上一切正常。

Code:代码:

***String path =Environment.getExternalStorageDirectory().getAbsolutePath() + "/My_directory/";
    File temp_file = new File(path);
    if (!temp_file.exists()){
        Boolean can_create= temp_file.mkdir();
    }***

the above code returns true in case of Android 10 or lower.如果 Android 10 或更低,上述代码返回 true。 but returns false in case of Android 11. Manifest permissions:但在 Android 11 的情况下返回 false。清单权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Noting that runtime permission is considered for same (READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE).请注意,运行时权限被视为相同(READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE)。

Manifest application:清单应用:

    <application
    android:requestLegacyExternalStorage="true"

The only way I am able to write in external storage is using getExternalFilesDir() but this is not the root directory.我能够在外部存储中写入的唯一方法是使用getExternalFilesDir()但这不是根目录。

according to thisdeveloper website, we can't create folders any more in the root directory!根据这个开发者网站,我们不能再在根目录中创建文件夹了!

Questions so far after checking this :检查后到目前为止的问题:

1- Is it confirmed that in Android 11 we can't create any folder on the root directory? 1-是否确认在 Android 11 中我们不能在根目录上创建任何文件夹? any work around?有什么解决方法吗?

2-If yes, what is the way forward to save data in external storage, excluding getExternalFilesDir() ? 2-如果是,将数据保存在外部存储中的方法是什么,不包括getExternalFilesDir()

3- Why android:requestLegacyExternalStorage="true" is not working? 3- 为什么android:requestLegacyExternalStorage="true"不起作用?

***String path =Environment.getExternalStorageDirectory().getAbsolutePath() + "/My_directory/"; ***字符串路径 =Environment.getExternalStorageDirectory().getAbsolutePath() + "/My_directory/";

You cannot create directories in root of external storage on Android 11 devices.您不能在 Android 11 设备上的外部存储的根目录中创建目录。

Instead create your own directories in one of the public directories of external storage.而是在外部存储的公共目录之一中创建您自己的目录。

File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "My_directory");

Why android:requestLegacyExternalStorage="true" is not working?为什么android:requestLegacyExternalStorage="true"不起作用?

android:requestLegacyExternalStorage is no longer works on Android 11+. android:requestLegacyExternalStorage不再适用于 Android 11+。 It is just a helper on Android 10 to give developers more time before migrating to scoped storage.它只是 Android 10 上的一个帮手,让开发人员在迁移到范围存储之前有更多时间。 On scoped storage, you need to use URI for creating, renaming, moving files, etc. Thus java.io.File is almost useless now.在范围存储上,您需要使用 URI 来创建、重命名、移动文件等。因此java.io.File现在几乎没用了。

Is it confirmed that in Android 11 we can't create any folder on the root directory?是否确认在 Android 11 中我们无法在根目录上创建任何文件夹? any workaround?任何解决方法?

No workaround.没有解决方法。

BTW, to reduce scoped storage complexity, I've created a library named Simple Storage .顺便说一句,为了降低范围存储的复杂性,我创建了一个名为Simple Storage的库。 It works across API levels.它适用于 API 级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Environment.getExternalStorageDirectory()在Android中不起作用 - Environment.getExternalStorageDirectory() not working in Android android Environment.getExternalStorageDirectory()。getPath() - android Environment.getExternalStorageDirectory().getPath() Android Environment.getExternalStorageDirectory()无法正常工作? - Android Environment.getExternalStorageDirectory() not work? Android 如何使用 Environment.getExternalStorageDirectory() - Android how to use Environment.getExternalStorageDirectory() Android 4.2 - Environment.getExternalStorageDirectory()。getPath()行为 - Android 4.2 - Environment.getExternalStorageDirectory().getPath() behaviour Android如何正确使用Environment.getExternalStorageDirectory() - Android how to use Environment.getExternalStorageDirectory() corretly Android 设备与 Environment.getExternalStorageDirectory()?= /mnt/sdcard/? - Android devices with Environment.getExternalStorageDirectory() != /mnt/sdcard/? 针对Environment.getExternalStorageDirectory()方法的不完整的Android文档 - Incomplete Android documentation for Environment.getExternalStorageDirectory() method Environment.getExternalStorageDirectory()在Android 4.1中不起作用 - Environment.getExternalStorageDirectory() is not working in Android 4.1 Environment.getExternalStorageDirectory()找不到文件android - Environment.getExternalStorageDirectory() not finding file android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM