简体   繁体   English

列出android中所有类型的文件

[英]List all files of one type in android

I need to retrieve a list of all files of a certain type on my internal and external storage. 我需要检索内部和外部存储上的某种类型的所有文件的列表。 I found this (-> List all of one file type on Android device? ) example, but it's very mp3 specific. 我发现这个( - > 在Android设备上列出所有一种文件类型? )的例子,但它非常特定于mp3。

My app creates a .zip file which is renamed to the extension *.message 我的应用创建了一个.zip文件,该文件被重命名为扩展名* .message

Another activity should display a list of all available .message files, from which the user can choose one to open it. 另一个活动应该显示所有可用的.message文件的列表,用户可以从中选择一个来打开它。

Does anyone has an idea how to begin? 有没有人知道如何开始?

Thanks! 谢谢!

To obtain a list of files with a specific extension you can use File.list() with a FilenameFilter . 要获取具有特定扩展名的文件列表,可以将File.list()FilenameFilter一起使用。 For example: 例如:

File dir = new File(".");

String[] names = dir.list(
    new FilenameFilter()
    {
        public boolean accept(File dir, String name)
        {
            return name.endsWith(".message");
        }
    });

Note that the returned strings are file names only, they do not contain the full path. 请注意,返回的字符串只是文件名,它们不包含完整路径。

Here it is 这里是

File root = new File(globalVar.recordDir);

{
    try {
        if (root.listFiles().length > 0) {
            for (File file : root.listFiles()) {
                if (file.getName().endsWith(".mp3")) {
                    //DO somthings with it
                } 
                }
            }
        } else {
        }
    } catch (Exception e) {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM