private ArrayList mFiles = null;
private final String ROOT = "/";
// main file browse function
private void browse(String path) {
mFiles.clear(); // clean previous file list
mPath.setText(path); // a TextView, record file path
File folder = new File(path);
File[] files = folder.listFiles();
if(files != null) {
for(File f : files) {
// add file name to list
if(f.isDirectory()) {
mFiles.add(f.getName() + "/");
}
else {
mFiles.add(f.getName());
}
}
}
ArrayAdapter fileAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mFiles);
mList.setAdapter(fileAdapter);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch(id) {
case R.id.home:
browse(ROOT);
break;
case R.id.back:
String current = (String) mPath.getText();
if(!current.equals(ROOT)) {
// get parent folder location
String parent = current.substring(0, current.lastIndexOf("/", current.length()-2)+1);
browse(parent);
}
break;
}
}
@Override
public void onItemClick(AdapterView l, View v, int position, long id) {
String path = mPath.getText() + mFiles.get(position);
File file = new File(path);
if(file.isDirectory()) {
file = null;
browse(path);
}
else {
// do something you wants
}
}
2012年3月29日 星期四
Android Simple File Browser
訂閱:
文章 (Atom)