Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the path to SD card

Please read the whole post before down-voting and/or marking it as a duplicate!

I'm working on an app that reads files from within a specific folder on the user's phone - either from the SD card (if there's one) or from the built in storage. Yes, the "READ_EXTERNAL_STORAGE" is mentioned in the manifest and I'm also handling the permission popup for API>23.

I used to simply use

File folder = new File(Environment.getExternalStorageDirectory(), "myfolder");

to get the path of the folder that is stored in the built in storage (32gb for an S7) but now I want to get the path to the SD card. According to pretty much every result google gave me, "Environment.getExternalStorageDirectory()" is supposed to give you the path to the SD card but for me it doesn't (and never has).

I've tested the following with two different Samsung Galaxy S7s, both with Android 7.0, one with an SD card (+ the folder), the other without (+ the folder):

Log.d(tag, System.getenv("EXTERNAL_STORAGE"));
Log.d(tag, System.getenv("SECONDARY_STORAGE"));
Log.d(tag, ""+new File(System.getenv("EXTERNAL_STORAGE")+File.separator+"myfolder").isDirectory());
Log.e(tag, ""+new File(System.getenv("EXTERNAL_STORAGE")+File.separator+ordner).getAbsolutePath());
Log.d(tag, Environment.isExternalStorageRemovable());
Log.d(tag, Environment.getExternalStorageDirectory());
Log.d(tag, Environment.getExternalStorageDirectory().getAbsolutePath());

To my surprise both phones output the same infos:

/sdcard
null
true
/sdcard/myfolder
false
/storage/emulated/0
/storage/emulated/0

According to the file manager app ("My Files"), the built in storage is called "Internal Storage", which makes even less sense (I know the difference between Internal and External Storage in Android).

How do I get the path to the actual SD card (without hardcoding it)?

like image 952
Neph Avatar asked May 02 '18 10:05

Neph


People also ask

Where is the SD card location on the device?

However, when the device features both, an internal and external SDCard, the latter usually is to be found "inside" the former, e.g. at /sdcard/external_sd. Alternatively, it may be mounted at /mnt/extSdCard or /storage/extSdCard. Note the capitals. Android filenames are case-sensitive.

Does getexternalstoragedirectory return the path to the SD card?

Environment.getExternalStorageDirectory () will NOT return path to micro SD card Storage. By sd card, I am assuming that, you meant removable micro SD card. In API level 19 i.e. in Android version 4.4 Kitkat, they have added File [] getExternalFilesDirs (String type) in Context Class that allows apps to store data/files in micro SD cards.

How do I insert the SD card into my Device?

To correctly insert the SD card, open the memory card cover (if present) and slide the card in by pushing the center portion of the bottom edge until it clicks into place at the back of the slot.

What is the default name of the external SD card?

Actually in some devices external sdcard default name is showing as extSdCard and for other it is sdcard1. This code snippet helps to find out that exact path and helps to retrive you the path of external device..


1 Answers

The only way I found is to semi-hardcode it:

File[] folders = myappcontext.getExternalCacheDirs();

gives you the path to the "cache" folders your app has access to (but that are deleted when you uninstall your app).

If the phone uses a removable SD card (that is currently mounted), the length of the array should be "2":

  1. The path to the "cache" folder in the external (not removable) storage
  2. The path to the "cache" folder on your SD card

They look something like this:

/storage/emulated/0/Android/data/com.mycompany.myapp/cache
/storage/xxxx-xxxx/Android/data/com.mycompany.myapp/cache

... where "x" is the number (id?) of your sd card. I've only been able to test it with 2 different SD cards and both had their own number.

Environment.getExternalStorageDirectory();

should also give you

/storage/emulated/0/

which is the non-hardcoding way of getting access to the external storage. ;)

If you create a new folder on the very first level of your SD card on your PC, its path will be:

/storage/xxxx-xxxx/myfolder

I also have to warn you: While you can read the "myfolder" folder, you can't write in it (will just throw an "Access Denied" exception with Android 7) because of the changes to the whole system that came with Kitkat. But that's a different problem I'm going to address in a new question.

like image 171
Neph Avatar answered Oct 03 '22 19:10

Neph