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?
from Allow user to switch between internal and external storage locations on Android
No comments:
Post a Comment