[英]How to find all files with certain extension on Android?
I'm using a fileBrowser to find the files on the phone, but I wanted to show all files that my app can open to the user, and then the user chooses one.我正在使用 fileBrowser 在手机上查找文件,但我想向用户显示我的应用程序可以打开的所有文件,然后用户选择一个。 Like the Music Player, that show all songs on phone, on the sdcard and in the internal memory, not only the ones in the folder where the user is.
就像音乐播放器一样,它可以显示手机、SD 卡和内部 memory 中的所有歌曲,而不仅仅是用户所在文件夹中的歌曲。
Use file name filters while listing out the files.列出文件时使用文件名过滤器。 The below sample lists out all mp3 files in a given
root
directory (Note - The below code doesn't do it recursively for all folders under root
) -下面的示例列出了给定
root
目录中的所有 mp3 文件(注意 - 下面的代码不会递归地对root
下的所有文件夹执行此操作) -
String files[] = root.list(audioFilter);
FilenameFilter audioFilter = new FilenameFilter() {
File f;
public boolean accept(File dir, String name) {
if(name.endsWith(".mp3") || name.endsWith(".MP3")) {
return true;
}
f = new File(dir.getAbsolutePath()+"/"+name);
return f.isDirectory();
}
};
I don't know what FileBrowser implementation you are using but a good one should accept a FileFilter .我不知道您使用的是什么 FileBrowser 实现,但一个好的实现应该接受FileFilter 。 You can implement your own filter providing code for
public abstract boolean accept (File pathname)
您可以实现自己的过滤器,为
public abstract boolean accept (File pathname)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.