简体   繁体   English

无法在Android 11中创建文件夹(目录),如何创建?

[英]Unable to create a folder(directory) in Android 11, how to create it?

I am unable to create a folder in my Android 11 device using java.我无法使用 java 在我的 Android 11 设备中创建文件夹。 I used this piece of code我使用了这段代码

 File file = new File(Environment.getExternalStorageDirectory() + "/folderName/folderName1");
        if (!file.mkdirs()) {
            file.mkdirs();
        }
String filePath = file.getAbsolutePath() + File.separator + "demoName";
        Log.i("filePath",filePath.toString());

The output of above log statement is /storage/emulated/0/folderName/folderName1/demoName but when I check my phone there is no such folder created.上述日志语句的 output 是/storage/emulated/0/folderName/folderName1/demoName但是当我检查我的手机时没有创建这样的文件夹。

Just to confirm I have researched and did include everything I need to include in the Manifest file.只是为了确认我已经研究并确实包含了我需要包含在清单文件中的所有内容。

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

And I have also placed我也放置了

 android:requestLegacyExternalStorage="true"

in the correct place inside of the application tag.在应用程序标签内的正确位置。

This is what I do to download a PDF from a link.这就是我从链接下载 PDF 的方法。

 File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/Aide Memoire");

    public  void DownloadBooks(String url,String title){

        DownloadManager.Request request=new DownloadManager.Request(Uri.parse(url));
        String tempTitle=title.trim();
        request.setTitle(tempTitle);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB){
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        String filePath = file.getAbsolutePath();
        Log.i("filePath",filePath.toString());
        request.setDestinationInExternalPublicDir(filePath,tempTitle+".pdf");
        Toast.makeText(this, "Saved to " + filePath , Toast.LENGTH_SHORT).show();
        DownloadManager downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
        request.setMimeType("application/pdf");
        request.allowScanningByMediaScanner();
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        downloadManager.enqueue(request);
    }
DownloadBooks("a link"," a name for the file");

After running the above I get an error and the logcat says运行上述内容后,我收到一个错误,logcat 说

java.lang.IllegalStateException: Not one of standard directories: 

Now, how do I create a custom folder in Internal Storage in Android 11 without error?现在,如何在 Android 11 的内部存储中创建自定义文件夹而不会出错?

are you check storage permission in runtime?你在运行时检查存储权限吗? here is the api 30 storage access that google changed in android 11:这是谷歌在 android 11 中更改的 api 30 存储访问:

Media store 媒体商店

Well thats called external storage as you can see on that function name.这就是所谓的外部存储,正如您在 function 名称上所见。

But on an Android 11 device you cannot create your own subdirectories on root of external storage.但是在 Android 11 设备上,您无法在外部存储的根目录上创建自己的子目录。

Instead do it on one of the public directories that are already there like Documents, DCIM, Pictures and so on而是在已经存在的公共目录之一上执行此操作,例如 Documents、DCIM、Pictures 等

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM