gpt4 book ai didi

android - 使用 FileFilter 的 ListActivity

转载 作者:行者123 更新时间:2023-11-29 22:13:09 24 4
gpt4 key购买 nike

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

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));
}

最佳答案

我认为问题出在以下行:

    if ((pathname.isDirectory() || pathname.isFile()) && !pathname.isHidden()){
return true;
}

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

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

    if (pathname.isDirectory() && !pathname.isHidden()) {
return true;
}

关于android - 使用 FileFilter 的 ListActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9132110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com