如何在android studio的内部存储中为andoidx创建文件夹?

问题描述:

如何在 android studio 的内部存储中创建文件夹并在所有 android 版本中工作,直到 android Q.

how to create folder in internal storage in android studio and works in all android versions till android Q.

AndroidManifest.xml

在清单文件中添加权限

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

为应用添加请求遗留外部存储

add request legacy external storage for application

<application
            android:requestLegacyExternalStorage="true">
</application>

MainActivity.this

添加此代码 MainActivity.this 文件

add this code MainActivity.this file

File f = new File(Environment.getExternalStorageDirectory(), "My folder");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            try {
               Files.createDirectory(Paths.get(f.getAbsolutePath()));
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }
        } else {
            f.mkdir();
            f.mkdirs();
            Toast.makeText(getApplicationContext(), f.getPath(), Toast.LENGTH_LONG).show();
        }