主程序:
private List<String> items = null;
private List<String> paths = null;
private String rootPath = "/";
private TextView mPath;
private ListView listView1;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.file_browser);
mPath = (TextView) findViewById(R.id.textView1);
listView1 = (ListView) findViewById(R.id.listView1);
getFileDir(rootPath);
}
/* 取得文件架构的method */
private void getFileDir(String filePath) {
/* 设定目前所在路径 */
mPath.setText(filePath);
items = new ArrayList<String>();
paths = new ArrayList<String>();
File f = new File(filePath);
File[] files = f.listFiles();
if (!filePath.equals(rootPath)) {
/* 第一笔设定为[回到根目录] */
items.add("返回根目录");
paths.add(rootPath);
/* 第二笔设定为[回上层] */
items.add("返回上一层");
paths.add(f.getParent());
}
/* 将所有文件加入ArrayList中 */
for (int i = 0; i < files.length; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
/*
* 声明一ArrayAdapter,使用file_row这个Layout, 并将Adapter设定给此ListActivity
*/
MyListAdapter myListAdapter = new MyListAdapter(this ,items , paths);
listView1.setAdapter(myListAdapter);
listView1.setOnItemClickListener(new ListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
File file = new File(paths.get(arg2));
if (file.canRead()) {
if (file.isDirectory()) {
/* 如果是文件夹就再进去读取 */
getFileDir(paths.get(arg2));
} else {
openFile(file);//自定义打开文件的方法
}
} else {
/* 弹出AlertDialog显示权限不足 */
new AlertDialog.Builder(IconFileBrowser.this)
.setTitle("Message")
.setMessage("权限不足!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
}
}).show();
}
}
});
MyListAdapter.class
public class MyListAdapter extends BaseAdapter{
private Context context;
private List<String> fileName , filePath;
public MyListAdapter(Context context , List<String> fileName , List<String> filePath){
this.context = context;
this.fileName = fileName;
this.filePath = filePath;
}
public int getCount() {
return fileName.size();
}
public Object getItem(int position) {
return fileName.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);//LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.list_item3 , null);
ImageView image = (ImageView) layout.findViewById(R.id.imageView1);
File file = new File(filePath.get(position));
if(file.isDirectory()){
image.setImageResource(R.drawable.folder);
}
else image.setImageResource(R.drawable.doc);
TextView title = (TextView) layout.findViewById(R.id.textView1);
title.setText(fileName.get(position));
return layout;
}
}
list_item3.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView1" android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout> </LinearLayout>
识别文件类型并打开
private void openFile(File f) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
/* 调用getMIMEType()来取得MimeType */
String type = getMIMEType(f);
/* 设定intent的file与MimeType */
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
}
/* 判断文件MimeType的method */
private String getMIMEType(File f) {
String type = "";
String fName = f.getName();
/* 取得扩展名 */
String end = fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();
/* 依扩展名的类型决定MimeType */
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
type = "audio";
} else if (end.equals("3gp") || end.equals("mp4")) {
type = "video";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
type = "image";
} else {
type = "*";
}
/* 如果无法直接打开,就弹出软件列表给用户选择 */
type += "/*";
return type;
}
重命名文件
file.renameTo(new File(newPath));
删除文件
file.delete();