无法在 /storage/emulated/0/Android/data/myproject.data/data 中创建目录 android 11

Not able to create directory in /storage/emulated/0/Android/data/myproject.data/data in android 11

我正在尝试在运行时创建目录,但抛出文件未找到异常

我要创建的目录:/storage/emulated/0/Android/data/myproject.data/data

Android 11 和 sdk android-30.0.3

static final String MAIN_DIR = "/storage/emulated/0/Android/data/myproject.data";
String logReportPath = MAIN_DIR+"/logcat";
File logDirectory = new File(logReportPath);
if(!logDirectory.exists())
{

  if(logDirectory.mkdirs()){
 
  }else{

   // always returning false and coming to this else condition .

  }

}else
{

}

请问为什么无法在运行时在存储中创建目录?

试试这个代码:

  String logReportPath = "logcat";
    File fileAddress = getApplicationContext().getExternalFilesDir(logReportPath); //storage/emulated/0/Android/data/YOUR_PROJECT/files/logcat
    if(!fileAddress.exists()) {
        fileAddress.mkdirs();
    }else {
        Log.i("INFO","directory or file not found");
    }

对你有帮助。