2012-02-03 9 views
1

チュートリアルを拡張しようとしたところ、簡単なファイルブラウザの作成についてhereが見つかりました。私はオーディオファイルだけを見るためにFileFilterを追加したかったのです。しかし、私はまだ私の文字列配列で定義したもの以外の他のファイルタイプを見ている。私が得ることができるすべての助けを感謝する。FileFilterを使用したListActivity

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

}

答えて

2

私はこの問題は、次の行にあると思う:

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

問題は、ロジックが言っているであるパスは、ディレクトリやファイルで、その後のパスです隠されていない。おそらく、パスがディレクトリかどうかを調べたいだけです。

を次のようにそれを変更してみてください:

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

ああヘクタール!それはありがたいことでした。私をしばらく夢中にしていた。 :) – joelreeves

関連する問題