public class SDFileExplorer extends AppCompatActivity {
ListView listView ;
TextView textView;
//父文件夹
File currentParent;
//父文件夹下所以文件
File[] currentFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.path);
Button bn = (Button)findViewById(R.id.parent);
listView = (ListView)findViewById(R.id.list);
File root = new File("/mnt/sdcard/");
if(root.exists()){
currentParent = root;
currentFiles = root.listFiles();
inflateListView(currentFiles); //填充列表
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击的选项不是文件夹
if(!currentFiles[position].isDirectory()) return;
//如果该文件夹下没有文件
File[] tmp = currentFiles[position].listFiles();
if(tmp == null || tmp.length==0){
Toast.makeText(SDFileExplorer.this,"该路径下没有文件",5000).show();
}
else {
currentParent = currentFiles[position];
currentFiles = tmp;
inflateListView(currentFiles);
}
}
});
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
if(!currentParent.getCanonicalPath().equals("/mnt/sdcard")){
currentParent = currentParent.getParentFile();
currentFiles = currentParent.listFiles();
inflateListView(currentFiles);
}
}catch (IOException e){
e.printStackTrace();
}
}
});
}
private void inflateListView(File[] files){
List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>();
for(int i=0;i<files.length;i++){
Map<String,Object> listItem = new HashMap<String,Object>();
if(files[i].isDirectory())
listItem.put("icon",R.drawable.folder);
else
listItem.put("icon",R.drawable.file);
listItem.put("fileName",files[i].getName());
listItems.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,R.layout.line,
new String[]{"icon","fileName"},new int[]{R.id.icon,R.id.file_name});
listView.setAdapter(simpleAdapter);
try{
textView.setText("当前路径:"+currentParent.getCanonicalPath());
}catch (IOException e){
e.printStackTrace();
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 显示当前路径 -->
<TextView
android:id="@+id/path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#000"/>
<Button
android:id="@+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:layout_gravity="center"
android:text="返回上一层"/>
</LinearLayout>
line.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义一个ImageView,用于作为列表项的一部分。 -->
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
/>
<!-- 定义一个TextView,用于作为列表项的一部分。 -->
<TextView android:id="@+id/file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
/>
</LinearLayout>
记得注册表上添加对sd卡的读取权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>