Reputation: 2090
getExternalStorageDirectory()
return SD card path on my phone. (Huawei Y320 - android 4.2.2).
now, how to get path Phone Storage path for all devices and all API? loot at bottom screenshot.
Upvotes: 4
Views: 9649
Reputation: 372
This function ensures to get all canonical readable storage paths on each android version (hopefully).
private static ArrayList<String> getAllStorages(Context context)
{
ArrayList<String> storagePaths = new ArrayList<>();
storagePaths.add(Environment.getExternalStorageDirectory().getAbsolutePath());
try
{
File[] externalDirs = context.getExternalFilesDirs(null);
if (externalDirs != null)
for (File file : externalDirs)
{
storagePaths.add(file.getAbsolutePath());
}
}
catch (Throwable e)
{
// ignore
}
String externalStorage = System.getenv("SECONDARY_STORAGE");
if (externalStorage != null && !externalStorage.isEmpty())
{
String[] externalPaths = externalStorage.split(":");
for (String e:externalPaths)
{
storagePaths.add(e);
}
}
externalStorage = System.getenv("EXTERNAL_STORAGE");
if (externalStorage != null && !externalStorage.isEmpty())
{
String[] externalPaths = externalStorage.split(":");
for (String e:externalPaths)
{
storagePaths.add(e);
}
}
ArrayList<String> result=new ArrayList<>();
for (String s:storagePaths)
{
File f=new File(s);
File f2=f;
while (f2 != null)
{
if (f2.canRead())
{
f = f2;
}
f2 = f2.getParentFile();
}
try
{
f = f.getCanonicalFile();
}
catch (IOException e)
{
// ignore
}
s = f.getPath();
if (!result.contains(s))
{
result.add(s);
}
}
return result;
}
Tested on Android 11.
You can test it on each Android and give feedback in comments, so we can make it more compatible if isn't.
Upvotes: 1
Reputation: 12145
Android 21+:
val publicStorages = ContextCompat.getExternalFilesDirs(this, null).mapNotNull {
it?.parentFile?.parentFile?.parentFile?.parentFile
}
// paths:
// /storage/emulated/0
// /storage/12F7-270F
Android 29+:
val volumeNames = MediaStore.getExternalVolumeNames(context).toTypedArray()
val phoneSdCard: String = volumeNames[0]
// external_primary == /storage/emulated/0
val removableMicroSdCard: String = volumeNames[1]
// 12f7-270f == /storage/12F7-270F
more about 29+: MediaStore alternative for deprecated Context.externalMediaDirs?
Upvotes: 2
Reputation: 870
try this code to get all external storage path for all devices
File[] f = ContextCompat.getExternalFilesDirs(getApplicationContext(),null);
for (int i=0;i< f.length;i++)
{
String path = f[i].getParent().replace("/Android/data/","").replace(getPackageName(),"");
Log.d("DIRS",path); //sdcard and internal and usb
}
Upvotes: 2
Reputation: 1103
External directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
if you User data directory...
Environment.getDataDirectory()
Recommended reading : Android External Storage
Cheers!
Upvotes: 1
Reputation: 601
I'm using this method:
public static final String SD_CARD = "sdCard";
public static final String EXTERNAL_SD_CARD = "externalSdCard";
private static final String ENV_SECONDARY_STORAGE = "SECONDARY_STORAGE";
public static Map<String, File> getAllStorageLocations() {
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));
}
}
return storageLocations;
}
Upvotes: 0