布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sdcard_browser.MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/current_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<ListView
android:id="@+id/file_browser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/current_path"/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Activity代码:
public class MainActivity extends AppCompatActivity {
// 定义父文件
private File currentParent;
// 存放当前父文件中的所有子文件
private File currentFiles[];
// 用于显示当前父文件的路径
private TextView textView;
// 返回上一层按钮
private Button back;
// 定义ListView用于加载文件列表
private ListView fileListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileListView = (ListView) findViewById(R.id.file_browser);
back = (Button) (Button) findViewById(R.id.back);
textView = (TextView) findViewById(R.id.current_path);
// 获取SD卡目录文件
File root = new File("/mnt/sdcard");
if(root.exists()){
currentParent = root;
// listFiles()方法可得到当前文件下的所有文件
currentFiles = currentParent.listFiles();
filesInflateView(currentFiles);
}
fileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(currentFiles[position].isFile()) return;
File temp[] = currentFiles[position].listFiles();
if(temp == null || temp.length == 0){
Toast.makeText(MainActivity.this, "该文件夹为空或不可访问",
Toast.LENGTH_SHORT).show();
}else{
currentParent = currentFiles[position];
currentFiles = temp;
filesInflateView(currentFiles);
}
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// 若当前的父文件的目录不是SD卡目录
if(!currentParent.getCanonicalPath().equals("/storage/emulated/legacy")){
currentParent = currentParent.getParentFile();
currentFiles = currentParent.listFiles();
filesInflateView(currentFiles);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void filesInflateView(File currenFiles[]){
List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < currenFiles.length;i++){
HashMap<String,Object> map = new HashMap<String,Object>();
if(currentFiles[i].isDirectory()){
map.put("icon",R.drawable.file);
}else{
map.put("icon",R.drawable.sun_file);
}
map.put("fileName",currenFiles[i].getName());
listItems.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.file_item,
new String[]{"icon","fileName"},new int[]{R.id.fileImage,R.id.fileName});
fileListView.setAdapter(adapter);
try {
textView.setText("当前路径:"+currentParent.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行效果如下:
注:文件夹图片在windows上截的,哈哈~~~