如何在 android 11 中创建自定义文件夹(App 文件夹)

How can I make custom folder (App folder's) in android 11

  Im facing a problem when creating app custom folder. like 

com.app and storage/.hideFolder etc.

通过使用下面的一些方法 android 11 (SDK API 30) 设备

它工作正常,但在 android11 中。无法使用如下所示的方法使其即时运行

 public static String root= Environment.getExternalStorageDirectory().toString();
 public static final String app_hided_folder ="/.HidedAPPTop/";
 public static final String app_showing_folder ="/APPTop/";
 public static final String draft_app_folder= app_hided_folder +"Draft/";


  public static void make_directry(String path,Context context) {
    File dir = new File(path);

    if (!dir.exists())
        Toast.makeText(context,
                (dir.mkdirs() ? "Directory has been created" : "Directory not created"),
                Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(context, "Directory exists", Toast.LENGTH_SHORT).show();
}

函数调用

make_directry(Variables.app_hided_folder,getContext());

清单

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

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

第二题

public static String root= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS ).toString();

uri 是我从选择器的响应中得到的视频路径。

File video_file = new File(uri.getPath());
            Log.d(Variables.tag + " new path", video_file.getAbsolutePath());
            Functions.copyFile(video_file,
                    new File(Variables.gallery_resize_video));

Function calling of copyFile

public static void copyFile(File sourceFile, File destFile) throws IOException {


        if (!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }

ERROR : new path: /storage/emulated/0/Download/STop/MP4_20210128_225711.mp4 System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/.HidedTop/gallery_resize_video.mp4: open failed: EACCES (Permission denied)

应用程序在目的地崩溃 =

new FileOutputStream(destFile).getChannel();

通过使用下面的代码片段,我的问题就解决了。

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

.getExternalStoragePublicDirectory() 已弃用。