Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Apr 13, 2015 at 01:13 PM · pathroot

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
             }
 
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ivolotto · Mar 07, 2017 at 11:27 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Apr 17, 2015 at 11:36 AM 0
Share

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

avatar image WoozyBytes · Apr 17, 2015 at 11:43 AM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

iTween makes a weird start on the path 1 Answer

Automatic path to point & click game. 1 Answer

Dynamically Add Points to Spline 1 Answer

Game object follow path of other game object 2 Answers

NavMeshAgent start from current position and specific view 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges