问题描述
我写一个Android应用程序,媒体播放器,所以我想整个手机上的所有文件(即SD卡和手机内存)进行扫描。我可以从SD卡读取,但它不是根。也就是说,我可以从路径刚读 / SD卡/ [文件夹] /
,它工作正常,但如果我去 / SD卡/
应用程序崩溃。我怎样才能访问手机本身在SD卡中的所有文件,以及这些文件?
I am writing an Android mediaPlayer app, so I want to scan through all files on the entire phone (i.e. sdcard and phone memory). I can read from the sdcard, but not the root of it. That is, I can just read from the path /sdcard/[folder]/
and it works fine, but if I go to /sdcard/
the app crashes. How can I access all the files on the sdcard, as well as the files on the phone itself?
推荐答案
不要使用/ SD卡/路径。它不能保证所有的工作时间。
Never use the /sdcard/ path. it is not guaranteed to work all the time.
使用低于code得到的路径,SD卡目录。
Use below code to get the path to sdcard directory.
File root = Environment.getExternalStorageDirectory();
String rootPath= root.getPath();
从ROOTPATH位置,你可以建立的路径,在SD卡上的任何文件。例如,如果有在/DCIM/Camera/a.jpg一个图像,则绝对路径将是ROOTPATH +/DCIM/Camera/a.jpg
From rootPath location, you can build the path to any file on the SD Card. For example if there is an image at /DCIM/Camera/a.jpg, then absolute path would be rootPath + "/DCIM/Camera/a.jpg".
然而,列出在SD卡中的所有文件,可以使用下面的code
However to list all files in the SDCard, you can use the below code
String listOfFileNames[] = root.list(YOUR_FILTER);
listOfFileNames将拥有一切在present在SD卡上的文件名称和经过过滤器设置的条件。
listOfFileNames will have names of all the files that are present in the SD Card and pass the criteria set by filter.
假设你想列出MP3文件只,然后通过下面的过滤器类名列表()函数。
Suppose you want to list mp3 files only, then pass the below filter class name to list() function.
FilenameFilter mp3Filter = new FilenameFilter() {
File f;
public boolean accept(File dir, String name) {
if(name.endsWith(".mp3")){
return true;
}
f = new File(dir.getAbsolutePath()+"/"+name);
return f.isDirectory();
}
};
词shash
Shash
这篇关于阅读在Android文件系统中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!