Android如何从文件获取根目录(路径) [英] Android How to get the root directory from a file (path)

查看:2027
本文介绍了Android如何从文件获取根目录(路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取任何路径的根目录.如果我有文件路径 /storage/emulated/0/Android/media/com.google.android.talk/Ringtones/,对我来说,返回根目录"storage"的最佳方法是什么?

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?

我需要创建一种方法来返回android手机上所有音频文件的根文件.

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

谢谢.

推荐答案

您所建议的将起作用.一种方法是使用带有indexOf的子字符串.

What you are suggesting would work. One way would be to use substring with indexOf.

rootPath = string.substring(0, string.indexOf("/")); 

这篇关于Android如何从文件获取根目录(路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆