现在的位置: 首页 > 综合 > 正文

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

2014年11月07日 ⁄ 综合 ⁄ 共 1173字 ⁄ 字号 评论关闭
From:<a target=_blank href="http://blog.csdn.net/meiyoutongguo/article/details/9935437" target="_blank">http://blog.csdn.net/meiyoutongguo/article/details/9935437</a>
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 stub
				fileList = 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();*/
    		}
    	}
    }

 

 

抱歉!评论已关闭.