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)?
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.
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.
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.
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..
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":
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With