Android How to get the root directory from a file (path)

眉间皱痕 提交于 2019-12-13 06:45:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!