Just another site

Thought this was cool: Android 最简单的SD卡文件遍历程序

leave a comment »

package com.wenhao.test.sddemo;import;import;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainDemo extends Activity {    /** Called when the activity is first created. */        private Button button = null;    private File path;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                   button = (Button)findViewById(;                //检测SD卡是否存在        if (Environment.getExternalStorageState().equals(                Environment.MEDIA_MOUNTED)) {            path = Environment.getExternalStorageDirectory();        }else{            Toast.makeText(this, "没有SD卡", Toast.LENGTH_LONG).show();            finish();        }                button.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                // TODO Auto-generated method stub                                                getAllFiles(path);            }        });            }        // 遍历接收一个文件路径,然后把文件子目录中的所有文件遍历并输出来     private void getAllFiles(File root){          File files[] = root.listFiles();          if(files != null){              for (File f : files){                  if(f.isDirectory()){                      getAllFiles(f);                  }else{                      System.out.println(f);                  }              }          }      }        }


from 博客园_业精于勤,荒于嬉;行成于思,毁于随:


Written by cwyalpha

五月 19, 2012 在 9:08 上午

发表在 Uncategorized


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: