欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-29 19:58:52
android 遍历sdcard的文件夹并显示目录信息 private string mresult = new string(); private string...

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

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