下面是利用Java的File类开发一个SD卡文件浏览器,该程序直接使用/mnt/sdcard来访问系统的sd卡目录,然后通过listFile() 方法来获取指定目录下的全部文件和文件夹.
package com.test.sdfileexplorer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
ListView mListView;
TextView mTextView;
//记录当前的父文件夹
File currentParent;
//记录当前路径下的所有文件的文件数组
File[] currentFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list);
mTextView = (TextView) findViewById(R.id.path);
//获取系统的sd卡目录
File root = new File("/mnt/sdcard/");
//如果sd是存在
if (root.exists()) {
currentParent = root;
currentFiles = root.listFiles();
//使用当前wen文件目录下的全部文件,文件夹来填充 listview
inflateListView(currentFiles);
}
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//用户点击了文件.直接返回,不做处理
if (currentFiles[position].isFile()) {
Toast.makeText(MainActivity.this, "该文件被点击了", Toast.LENGTH_SHORT).show();
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;
//再次更新listview
inflateListView(currentFiles);
}
}
});
//获取上一级的目录的按钮
Button btnPre = (Button) findViewById(R.id.parent);
if (btnPre != null) {
btnPre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (!currentParent.getCanonicalPath().equals("/mnt/sdcard")) {
//获取上一级目录
currentParent = currentParent.getParentFile();
//列出当前目录下所有文件
currentFiles = currentParent.listFiles();
//再次更新listview
inflateListView(currentFiles);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
private void inflateListView(File[] files) {
//创建一个 list集合 list集合的元素是 map
List<Map<String, Object>> listItems = new ArrayList<>();
for (int i = 0; i < files.length; i++) {
Map<String, Object> listItem = new HashMap<>();
//如果当前的file 时文件夹,使用folder图标,否则使用 file图标
if (files[i].isDirectory()) {
listItem.put("icon", R.drawable.folder);
} else {
listItem.put("icon", R.drawable.file);
}
listItem.put("fileName", files[i].getName());
//添加list项
listItems.add(listItem);
}
//创建一个 SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, listItems,
R.layout.item, new String[]{"icon", "fileName"}, new int[]{R.id.icon, R.id.file_name});
//listview 设置adapter
mListView.setAdapter(adapter);
try {
mTextView.setText("当前路径: " + currentParent.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
布局界面
<?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.test.sdfileexplorer.MainActivity">
<!--显示当前路径的文本框-->
<TextView
android:id="@+id/path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Hello World!"/>
<!--列出当前路径下所有的文件的 listView-->
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/path"
android:divider="#000"
android:dividerHeight="1px"
></ListView>
<!--返回上一级目录-->
<Button
android:id="@+id/parent"
android:layout_width="38dp"
android:layout_height="34dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@drawable/home"/>
</RelativeLayout>