Get SDCard Root Path

Hi everyone,

I’ve been searching for quite a long time now and I’m turning to you in case someone could help me.

I have a Unity project for a mobile application in which I need to find all the MP3 files stored on my SD card.
When I use Application.persistentDataPath, I get the complete path of the project but not the root of the SD card (I also tried to use Directory.GetParent(string path)).
Since each device has its own SD card path (/mtn/sdcard, /storage/sdcard0,…),
is there a way to get that path without hardcoding it?

Thank you !

PS : I’ve thought of using a Java Plugin but I wanted to ask you guys first :o)

You Can Try To Use [Directory.GetCurrentDirectory()][1] Instead.

On **my **Android** device** it returns me the root **( "/" )**.

Regards.

I was having trouble finding the right path and search pattern with my app but I used a $10 asset called File Chooser Dialog Box from the Unity asset store that saved me a lot of time and trouble. The source code is short and easy to follow. Unity Asset Store - The Best Assets for Game Making

class AndroidJavaUti
{
private static string m_pkgName;
private static string m_sdCardPath;
public static AndroidJavaObject Activity
{
get
{
AndroidJavaClass jcPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
return jcPlayer.GetStatic(“currentActivity”);
}
}

        public static string CurrentPkgName
        {
            get
            {
                if (m_pkgName == null)
                    m_pkgName = Activity.Call<string>("getPackageName");
                return m_pkgName;
            }
        }


        public static string CurrentSDCardPath
        {
            get
            {
                if (m_sdCardPath == null)
                {
                    AndroidJavaClass jc = new AndroidJavaClass("android.os.Environment");
                    IntPtr getExternalStorageDirectoryMethod = AndroidJNI.GetStaticMethodID(jc.GetRawClass(), "getExternalStorageDirectory", "()Ljava/io/File;");
                    IntPtr file = AndroidJNI.CallStaticObjectMethod(jc.GetRawClass(), getExternalStorageDirectoryMethod, new jvalue[] { });
                    IntPtr getPathMethod = AndroidJNI.GetMethodID(AndroidJNI.GetObjectClass(file), "getPath", "()Ljava/lang/String;");
                    IntPtr path = AndroidJNI.CallObjectMethod(file,getPathMethod,new jvalue[] { });
                    m_sdCardPath = AndroidJNI.GetStringUTFChars(path);
                    AndroidJNI.DeleteLocalRef(file);
                    AndroidJNI.DeleteLocalRef(path);
                    Debug.Log("m_sdCardPath = " + m_sdCardPath);
                }
                return m_sdCardPath;
            }

        }
    }