How to create folder (Android R - Api 30)?

限于喜欢 提交于 2021-01-01 10:23:06

问题


Read that Android 11 has scoped storage, but I can't find any information, how can I create and use folder in /storage/emulated/0/ ? Old methods works only on api 29 and below :(


回答1:


On Android 11 the restrictions in Android 10 concerning access to external storage are much less.

Environment.getExternalStorageDirectory()

is readable again and

Environment.getExternalStoragePublicDirectory(...)

is writable for folders like Environment.DIRECTORY_DOCUMENTS and so on.

The Android OS is very picky using the right extensions for your files in most of those directories.




回答2:


Step 1:

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

Step 2:

Environment.getExternalStorageDirectory().absolutePath + "/your_folder_name"



回答3:


Since Android Q we can create folders in app specif storage. its path:

Android->data->package name->files->your folder

Use this:

File file;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    file = new File (this.getExternalFilesDir(null) + path);
} else {
    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
}

if (!file.exists()) {
    file.mkdirs();
}


来源:https://stackoverflow.com/questions/64016682/how-to-create-folder-android-r-api-30

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!