2

I am working on a project where I need to allow the user to read and write either from the external SD card or from the internal storage, but no matter what I try only one storage location is found.

I am using the Android emulator which has an android studio managed SD card and internal storage, which when loading solid explorer shows that they're available.

I am using the following code to get the storage locations:

File[] files = ContextCompat.getExternalFilesDirs(this, null);
        for (int i = 0; i < files.length; i++)
        {
            Log.d("BasePicker", "Name: " + files[i].getName() + " Path: " + files[i].getParentFile().getParentFile().getParentFile().getParentFile());
        }

The below outputs the following:

Name: files Path: /storage/emulated/0

I am doing getPatentFile() 4 times as I read that shows the root of each storage location, otherwise the path will be something like /storage/emulated/0/package_name/files

I then try and get the external storage location using the following:

Log.d("BasePicker", Environment.getExternalStorageDirectory().getPath());

but I am getting exactly the same storage path of `/storage/emulated/0'.

I've also tried the following:

Map<String, File> storageLocations = new HashMap<>(10);
        File sdCard = Environment.getExternalStorageDirectory();
        storageLocations.put(SD_CARD, sdCard);
        final String rawSecondaryStorage = System.getenv(ENV_SECONDARY_STORAGE);
        if (!TextUtils.isEmpty(rawSecondaryStorage)) {
            String[] externalCards = rawSecondaryStorage.split(":");
            for (int i = 0; i < externalCards.length; i++) {
                String path = externalCards[i];
                storageLocations.put(EXTERNAL_SD_CARD + String.format(i == 0 ? "" : "_%d", i), new File(path));
            }
        }

but this again only returns /storage/emulated/0

So the question is, how can I get the internal and external storage locations if they're available so that I can allow the user to select which storage location they want to use and they can browse and read/write to which ever storage location they want?

UPDATE

I've found by using the adb shell the file I create from solid explorer on my SD card is located in /mnt/media_rw/1AF7-1E14 but this seems to only be accessible as root, a normal user can't see it.

Boardy
  • 35,417
  • 104
  • 256
  • 447

1 Answers1

0

Did you check this post? https://stackoverflow.com/a/23949650/6318881 According to that post, System.getenv("SECONDARY_STORAGE") is what you are looking for.

If that doesn't work, I would get a list of the directories in /storage, emulated is your internal storage and the other folder should be the SD card.

Brent Thierens
  • 371
  • 2
  • 12
  • I have tried using `SECONDARY_STORAGE` its the static variable I used called `ENV_SECONDARY_STORAGE` but I get the same path as `getExternalStorageDirectory`. In storage I only have /emulated and /self which contains /primary but doesn't show what is on my SD card – Boardy Feb 24 '19 at 13:39
  • What version of Android are you using? – Brent Thierens Feb 24 '19 at 14:18
  • I checked your code and it is running fine for me. Both my SD and the internal storage showed up. Can you check whether the SD card is formatted as 'internal storage' or as 'removable storage'? – Brent Thierens Feb 25 '19 at 17:42
  • Hmm odd, how can I check how its formatted. Its an android emulator which is studio managed. – Boardy Feb 26 '19 at 17:46
  • I don't know about emulators, but if you have the settings app, I would go to settings > storage – Brent Thierens Feb 26 '19 at 18:46
  • Yea I found that, it didn't give me an option to choose either or, it just said it was going to format as external storage - I did that but made no difference – Boardy Feb 26 '19 at 19:06