简体   繁体   English

如何在 Android 上找到所有具有特定扩展名的文件?

[英]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.

相关问题 在项目的源文件夹中查找某个扩展名的所有文件 - Finding all the files of a certain extension in a project's source folder 如何在SDCard上找到所有音频文件(eclipse,android) - How to find all audio files on SDCard (eclipse, android) 如何查找所有文本文件,无论扩展名只包含逗号和数字? - How to find all textual files regardless of extension that contains only commas and digits? 如何在Eclipse中查找某个变量/方法的所有出现? 即相当于Android Studio的“ cmd + b” - How to find all occurrences of a certain variable/method in Eclipse? i.e the equivalent of “cmd + b” of Android Studio 如何从Andorid的文件夹中删除具有特定扩展名的文件 - How to delete files with a certain extension from a folder in Andorid 如何在Java中创建具有特定扩展名的文件的InputStream? - How to create an InputStream of files that have a certain extension in Java? 在android中查找存储中的所有mp3文件 - Find all mp3 files in storage in android Android-如何将一个扩展名的文件添加到ListView - Android - How to add files of one extension to listview 如何获取springboot应用程序资源中所有扩展名为xml的文件? - How to get all files with xml extension placed in resources of springboot application? 如何扫描所有驱动器以查找Java中的特定文件扩展名? - How to Scan all drives to find specific file extension in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM