Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get all external storage path for all devices

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.

enter image description here

like image 330
Farzad Avatar asked Oct 28 '25 16:10

Farzad


2 Answers

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
}
like image 199
milan pithadia Avatar answered Oct 31 '25 12:10

milan pithadia


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?

like image 43
user924 Avatar answered Oct 31 '25 11:10

user924



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!