3

How can i get extSdcard path in android?

There are 2 storage, first external storage in which all the phones have it but there is second storage which is called removable storage (micro sdcard).

I want to get the path to the micro sdcard in android, how is that possible?

Ali Akhgar
  • 101
  • 1
  • 10

4 Answers4

2

Starting from KitKat, you have access to a method to get that directory :

Context.getExternalFilesDirs()

Note that it may return null if the SD card is not mounted or moved during the access. Also, you might need to add these permissions to your manifest file :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Apparently some users had trouble with this method, not returning the SD card path. I found some useful informations on how it can works and how it can not depending on the device on this post.

YumeYume
  • 981
  • 2
  • 12
  • 33
  • is there any way for below kitkat? android 4.2+ – Ali Akhgar Jul 12 '16 at 09:50
  • You might find a solution here : http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0 . Also, the link provided by mani in his answer seems to contain valuable informations too. – YumeYume Jul 12 '16 at 10:01
  • 2
    This returns the emulated internal memory, not the removable SD card. – Tom Kincaid Oct 19 '16 at 16:58
  • @TomKincaid Is is correct. This does not get the removable SD Card path. How is this the accepted answer?! – Joshua Pinter Jul 21 '18 at 17:24
  • This method does return the sd card path, only if the phone's manufacturer allowed it (or didn't forget to). The method returns an ARRAY of directories, the first one being a part of the internal storage configured to act as "external", the second one being a removable path, as an SD card. An answer that isn't giving you copypastable code and forces you to read some documentation doesn't mean that it's wrong. See the link added in the answer for more information. – YumeYume Jul 23 '18 at 07:35
2

Check this https://developer.android.com/guide/topics/data/data-storage.html#filesExternal for complete reference as you need to define permission to read and write to the external SD also there is code in the above link which checks if SD card is available before performing any action

mdb
  • 166
  • 8
0

The following works for me!

File[] Dirs = ContextCompat.getExternalFilesDirs(this.getApplicationContext(), null);
File path = (Dirs[1]);
String pathSD = Dirs[1].toString();
int firstOpen = pathSD.indexOf("/");
int secondOpen = pathSD.indexOf("/", firstOpen + 1);
int thirdOpen = pathSD.indexOf("/", secondOpen + 1);
String filename = pathSD.substring(firstOpen, thirdOpen + 1);
path = new File(filename);
0

As of API level 24, you can use the StorageManager class:

https://developer.android.com/reference/android/os/storage/StorageManager#getStorageVolumes()

Nick
  • 645
  • 5
  • 11