0

I am unable to create a folder in my Android 11 device using java. 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.

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.

 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

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

Now, how do I create a custom folder in Internal Storage in Android 11 without error ?

Randy Orton
  • 33
  • 1
  • 7
  • Requesting legacy external storage is only for Android 10 devices. – blackapps May 05 '21 at 09:46
  • `request.setDestinationInExternalPublicDir(filePath,tempTitle+".pdf")` Dont mess around with a File class path. `request.setDestinationInExternalPublicDir(Environment.DIRECTORY.DOCUMENTS,tempTitle+".pdf")`. – blackapps May 05 '21 at 18:51
  • Or `request.setDestinationInExternalPublicDir(Environment.DIRECTORY.DOCUMENTS, "Aide Memoire/" +tempTitle+".pdf")`. – blackapps May 05 '21 at 18:53
  • So, does that indicates that the files(mostly documents) could only get downloaded in the DOCUMENTS folder and not in a nested folder within the DOCUMENTS folder ? – Randy Orton May 05 '21 at 19:07
  • Why are you asking? Didnt you see that i used "Aide Memoires" too? And didnt you try all? And why dont you report if it works? – blackapps May 05 '21 at 21:18
  • That indeed does work, I just caught myself up :). Thank you @blackapps – Randy Orton May 05 '21 at 21:29
  • I found a working solution https://stackoverflow.com/questions/67367047/android-11-primary-directory-invalid-not-allowed-for-content-media-external/67408905#67408905 – Aykut Uludağ May 05 '21 at 21:56

2 Answers2

0

are you check storage permission in runtime? here is the api 30 storage access that google changed in android 11:

Media store

Mohammad Mirdar
  • 56
  • 1
  • 11
0

Well thats called external storage as you can see on that function name.

But on an Android 11 device you cannot create your own subdirectories on root of external storage.

Instead do it on one of the public directories that are already there like Documents, DCIM, Pictures and so on

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Instead of `Environment.getExternalStorageDirectory()` I used `File file = new File(Environment.DIRECTORY_ALARMS + "/folderName/folderName1"); ` but it did not create a folder in the public directory ALARMS. How to do it ? – Randy Orton May 05 '21 at 12:42
  • Of course not that is no valid path. Which you could easily see for yourself inspecting file.getAbsolutePath(). And file.exists(). `File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS), "folderName/folderName1");` – blackapps May 05 '21 at 13:20
  • Be aware that Android 11 is very picky concerning file extensions. For example you cannot create files with extension .pdf in Alarms public dir. An .mp3 is possible. – blackapps May 05 '21 at 13:24
  • Your code does makes a new directory in the folder and considering your advice , for **PDF's** I choose the documents folder, but when I download the file I get an error, this is what the logcat says `java.lang.IllegalStateException: Not one of standard directories: /storage/emulated/0/Documents/My Custom Folder ` and the app gets crashed and hence nothing gets stored.**Not** how do I work this out so as that the downloaded pdf gets stored in `/storage/emulated/0/Documents/My Custom Folder ` – Randy Orton May 05 '21 at 14:34
  • Post your download code in your post. How would we know what you are doing ? – blackapps May 05 '21 at 15:44
  • I have included the code in my post and edited it. You may check it :) – Randy Orton May 05 '21 at 18:25