首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用FileFilter的ListActivity

使用FileFilter的ListActivity
EN

Stack Overflow用户
提问于 2012-02-04 00:39:11
回答 1查看 2.8K关注 0票数 1

我正在尝试扩展我发现的关于创建一个简单的文件浏览器的here教程。我想添加一个只能看到音频文件的FileFilter。但是,我仍然看到除了我在字符串数组中定义的文件类型之外的其他文件类型。感谢我能得到的任何帮助。

代码语言:javascript
运行
复制
public class SDCardExplorer extends ListActivity {

private String mediapath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());

private List<String> item = null;
private List<String> path = null;

private TextView mypath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.medialist);

    mypath = (TextView) findViewById(R.id.mypath);

    LoadDirectory(mediapath);
}

// class to limit the choices shown when browsing to SD card to media files
public class AudioFilter implements FileFilter {

    // only want to see the following audio file types
    private String[] extension = {".aac", ".mp3", ".wav", ".ogg", ".midi", ".3gp", ".mp4", ".m4a", ".amr", ".flac"};

    @Override
    public boolean accept(File pathname) {

        // if we are looking at a directory/file that's not hidden we want to see it so return TRUE
        if ((pathname.isDirectory() || pathname.isFile()) && !pathname.isHidden()){
            return true;
        }

        // loops through and determines the extension of all files in the directory
        // returns TRUE to only show the audio files defined in the String[] extension array
        for (String ext : extension) {
            if (pathname.getName().toLowerCase().endsWith(ext)) {
                return true;
            }
        }

        return false;
    }      
}

private void LoadDirectory(String dirPath) {      

    mypath.setText("Location: " + dirPath);

    item = new ArrayList<String>();
    path = new ArrayList<String>();

    File f = new File(dirPath);
    File[] files = f.listFiles(new AudioFilter());

    // If we aren't in the SD card root directory, add "Up" to go back to previous folder
    if(!dirPath.equals(mediapath)) {

        item.add("Up");
        path.add(f.getParent());
    }

    // Loops through the files and lists them
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        path.add(file.getPath());

        // Add "/" to indicate you are looking at a folder
        if(file.isDirectory()) {
          item.add(file.getName() + "/");
        }
        else {
          item.add(file.getName());
        }  
    }

    // Displays the directory list on the screen
    setListAdapter(new ArrayAdapter<String>(this, R.layout.mediaitems, item));
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-04 00:45:07

我认为问题出在下面这一行:

代码语言:javascript
运行
复制
    if ((pathname.isDirectory() || pathname.isFile()) && !pathname.isHidden()){
        return true;
    }

问题是,逻辑是说路径是一个目录或一个文件,然后是路径不隐藏。您可能只想查找路径是否为目录。

尝试将其更改为以下内容:

代码语言:javascript
运行
复制
    if (pathname.isDirectory() && !pathname.isHidden()) {
        return true;
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9132110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档