5

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)?

11
  • Environment.getExternalStorageDirectory() can be used. keep following the parent folder until you find /sdcard/ Commented May 2, 2018 at 10:48
  • Please read the whole post! my first test already gave me "/sdcard" but even though I crated the "myfolder" folder directly inside it using the "My Files" app, the third test failed.
    – Neph
    Commented May 2, 2018 at 11:06
  • I can see you are using SECONDARY_STORAGE to check for your folder, not EXTERNAL_STORAGE Commented May 2, 2018 at 11:09
  • Go up one line. Like I said: Please read the whole post!
    – Neph
    Commented May 2, 2018 at 11:23
  • 1
    @LarsH It's a really similar question, you're right about that but it's 7 years old and about a much older Android version - I asked about Android 7.0, in which accessing files on SD cards works in a completely different way compared to e.g. Android 4.3.
    – Neph
    Commented Jan 25, 2019 at 9:42

2 Answers 2

10

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.

1
  • These are good ways (for lack of a better way) of finding the removable SD card. For a more robust approach, across more Android versions and manufacturers, see stackoverflow.com/questions/5694933/…
    – LarsH
    Commented Jan 29, 2019 at 10:23
-1

you have to insert the following permission into your application's manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android" >

   <!-- Add this -->
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

</manifest>
1
  • 1
    Please read the whole post! The permission is already mentioned in my manifest file (including the permission popup in my code), otherwise it wouldn't have worked in the first place before I tried changing it, so it can handle SD cards as well.
    – Neph
    Commented May 2, 2018 at 11:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.