android开发中实现对某一类型文件的sdcard扫描

来源:互联网 发布:淘宝号怎样会被冻结 编辑:程序博客网 时间:2024/06/05 17:47
public class TestActivity extends Activity {    /** Called when the activity is first created. */Button button1;List<String> fileList;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button1 = (Button)findViewById(R.id.button1);        button1.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubfileList = new ArrayList<String>();readFile();for(int i=0 ; i<fileList.size(); i++) {Log.i("syso", fileList.get(i).toString());}}});    }        private void readFile() {    final File[] file = new File("/etc").listFiles();//设定扫描路径    readFile(file);    }    private void readFile(final File[] file) {         for(int i=0 ; file!= null && i<file.length ;i++) {    //判读是否文件以及文件后缀名    if(file[i].isFile()/* && file[i].getName().endsWith("xml")*/){    fileList.add(file[i].toString());    }    //如果是文件夹,递归扫描    else if(file[i].isDirectory()) {    final File[] newFileList = new File(file[i].getAbsolutePath()).listFiles();    readFile(newFileList);    //通过多线程来加速/*new Thread(new Runnable() {                    public void run() {                        readFile(newFileList);                    }                }).start();*/    }    }    }}
原创粉丝点击