Android 遍历SDCARD的文件夹并显示目录信息

Android 遍历SDCARD的文件夹并显示目录信息

private String mResult = new String(); private String[] mFileList = null;

1)、显示所有文件/

File flist = new File("/mnt/sdcard");
  mFileList = flist.list();
  for(String str: mFileList){
    mResult += str;
    mResult += "\n";
  }
TextView tv = (TextView)findViewById(R.id.result);
tv.setText(mResult.toString());

2)、显示指定后缀名的文件/

FilenameFilter fnf = new FilenameFilter(){
    public boolean accept(File dir, String filename) {
      if(filename.endsWith(".mp3")) return true;
      return false;
    }
  };

  mFileList = flist.list(fnf);

  for(String str: mFileList){
    mResult += str;
    mResult += "\n";
  }

3)、仅显示目录/

FileFilter ff = new FileFilter(){
    public boolean accept(File pathname) {
      return pathname.isDirectory();
    }
  };
File[] fileDir = flist.listFiles(ff);
for (int i = 0; i < fileDir.length; i++) {
    String str = fileDir[i].getName();
    mResult += str;
    mResult += "\n";
  }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

你可能感兴趣的:(Android 遍历SDCARD的文件夹并显示目录信息)