1

I have referred this guide.

I want to create one folder say "XYZ" on the root of sdcard. So consider the following code: (XYZ folder does not exist on internal or external sd card)

    private void aaskForAppyStoreFolderPermission(){
    if(FWCompat.isNougat_24_OrNewer()){
        StorageManager sm = (StorageManager)getSystemService(Context.STORAGE_SERVICE);
        File f = new File("/XYZ");
        StorageVolume extStorage = sm.getStorageVolume(f);
        if(extStorage != null) {
            Intent intent = extStorage.createAccessIntent(null);
            startActivityForResult(intent, 100);
        }
    }
}

But this does not show any popup for accessing XYZ folder. Now even if I create a folder manually and try again with "/XYZ" or "/storage/emulated/0" or "/storage/459-C045/XYZ" path. It still does not work.

But if I try to get access for whole external memory card : as following then it shows the popup but the message says that do you wish to grant access the complete sd card (which is correct but not what I need. As I want access to only one particular folder (which may or may not exist)) The code to get access to whole memory card:

    private void askForAppyStoreFolderPermission(){
    if(FWCompat.isNougat_24_OrNewer()){
        StorageManager sm = (StorageManager)getSystemService(Context.STORAGE_SERVICE);
        List<StorageVolume> volumeList = sm.getStorageVolumes();
        if(volumeList != null && volumeList.size() > 0){
            StorageVolume extStorage = volumeList.get(volumeList.size() - 1);
            if(extStorage != null) {
                Intent intent = extStorage.createAccessIntent(null);
                startActivityForResult(intent, 100);
            }
        }
    }
}
Sasuke Uchiha
  • 890
  • 3
  • 15
  • 31

1 Answers1

1

Here it shows what directories you can get access to:

  • DIRECTORY_ALARMS
  • DIRECTORY_DCIM
  • DIRECTORY_DOCUMENTS
  • DIRECTORY_DOWNLOADS
  • DIRECTORY_MOVIES
  • DIRECTORY_MUSIC
  • DIRECTORY_NOTIFICATIONS
  • DIRECTORY_PICTURES
  • DIRECTORY_PODCASTS
  • DIRECTORY_RINGTONES

I wanted to do the same thing you tried to do (read/write to a folder at the root or create if it doesn't exist), unfortunately it's either one of these or the very root of the SD card (with createAccessIntent(null)).

From there you can then search through the tree to see if your folder exists and if it doesn't, create it directly at the root by doing something like:

Uri u = myintent.getData(); //pass this in onActivityResult(...)!
DocumentFile wholeSD = DocumentFile.fromTreeUri(myContext,u);
DocumentFile myFolder = wholeSD.findFile("myfolder");

if(myFolder.exists()) {
    if(myFolder.isDirectory()) {
        doStuff();
    }
} else {
    wholeSD.createDirectory("myFolder");
}

You can also try getting the actual path to the SD card (see my answer in this question) but there's no guarantee that it'll work on every phone, plus you only get reading access - as soon as you try to write something like you would in the normal external storage (non-removable/built in), it'll probably give you a "no access" exception.

Neph
  • 1,823
  • 2
  • 31
  • 69