- Home /
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)
Answer by nwpu053741 · Dec 09, 2016 at 06:20 AM
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<AndroidJavaObject>("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;
}
}
}
Sorry, I know this is an old post but... where do I have to put this code? Do I have to create a new javascript iside unity and copy the code inside it? Thanks
Answer by WoozyBytes · Apr 14, 2015 at 05:27 PM
You Can Try To Use Directory.GetCurrentDirectory() Instead.
On my Android device it returns me the root ( "/" ).
Regards.
Hello WoozyBytes and thank you for your concern :o)
Indeed, the solution you gave me brings me to the root path ! Here's my code line to find the mp3 files stored in it : mp3FilesPath = Directory.GetFiles (Directory.GetCurrentDirectory (), "*.mp3", SearchOption.AllDirectories);
However, this doesn't find anything (normally it should look for songs in all its subfolders right?) I'll keep on doing some research but I'd appreciate if you had any intel about that !
Regards
Hi there !!!
Personally i never used that method , i had to create my own File browser and let the user Get the files , but its a lot more work.
Anyway there will be some folder's that you will not have access to.
Do you get Any errors ??
If so You can try to try/catch them.
Regards.
Answer by BjoUnity3d · Nov 24, 2016 at 10:56 AM
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. https://www.assetstore.unity3d.com/en/#!/content/31546