main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>SDCardActivity
public class SDCardActivity extends Activity {
private static final String TAG = "SDCardActivity";
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.tv = (TextView) this.findViewById(R.id.tv);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
// 就這樣!
File sdCardDir = Environment.getExternalStorageDirectory();
// 只讀取 music 目錄下的檔案
File musicDir = new File(sdCardDir, "music");
File[] list = musicDir.listFiles();
StringBuilder sb = new StringBuilder();
this.listChildred(list, sb, "");
this.tv.setText(sb.toString());
}
private void listChildred(File[] list, StringBuilder sb, String space) {
if (list == null) {
// 跳過空目錄
Log.e(TAG, ">>>>>>>>>>>>>>>>");
return;
}
for (File f : list) {
String msg;
if (f.isDirectory()) {
msg = space + " + " + f.getName();
sb.append(msg + "\n");
Log.d(TAG, msg);
// 往下一層
this.listChildred(f.listFiles(), sb, space + " ");
}
else {
msg = space + " - " + f.getName();
sb.append(msg + "\n");
Log.d(TAG, msg);
}
}
}
}
相關文章
沒有留言:
張貼留言