问题
How do I get the root directory of any path. If i have file path /storage/emulated/0/Android/media/com.google.android.talk/Ringtones/, what is the best way for me to return the root directory "storage".
Is the file path always in "/" format, is that safe to split the path based on the character "/" or is there a built in function that I can call?
I need to create a method to return the root files of all the audio files on the android phone.
Here are some paths that I have and I want to start with the root directory and then browse each directory with audio files, so is there any build in method that I can call that will return the first directory?
/storage/emulated/0/media/audio/ringtones/abc.mp3
/storage/emulated/0/Music/Various Artists/music.mp3
/storage/emulated/0/new/Artists/test.mp3
Thanks.
回答1:
What you are suggesting would work. One way would be to use substring with indexOf.
rootPath = string.substring(0, string.indexOf("/"));
来源:https://stackoverflow.com/questions/39626455/android-how-to-get-the-root-directory-from-a-file-path