用Java的File类开发的一个SD卡文件浏览器
</pre><pre name="code" class="html">界面布局文件如下:
<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"
>
<!-- 显示当前路径的文本框 -->
<TextView
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"/>
<!-- 列出当前路径下所有文件的ListView -->
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="1px"
android:layout_below="@id/path"
/>
<Button
android:id="@+id/parent"
android:layout_width="38dp"
android:layout_height="34dp"
android:background="@drawable/home"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
该程序主要用了File的listFile来列出指定目录的全部文件
package com.example.sdfileexplorer;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView;
TextView textView;
File currentParent;
File[] currentFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.list);
textView=(TextView)findViewById(R.id.path);
//获取系统的SD卡目录
File root=new File("/mnt/sdcard/");
//如果SD卡存在
if(root.exists())
{
currentParent=root;
currentFiles=root.listFiles();
//使用当前目录下的全部文件,文件夹来填充listView
inflateListView(currentFiles);
}
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
//如果用户单击了文件,直接返回,不做任何处理
if (currentFiles[position].isFile())
return;
//获取用户点击的文件夹下的所有文件
File[] tmp=currentFiles[position].listFiles();
if(tmp==null||tmp.length==0)
{
Toast.makeText(MainActivity.this, "当前路径不可访问或该路径下没有文件", Toast.LENGTH_SHORT).show();
}
else
{
//获取用户单击的列表项对应的文件夹,设为当前父文件夹
currentParent=currentFiles[position];
//保存当前父文件夹下的全部文件和文件夹
currentFiles=tmp;
//再次更新listView
inflateListView(currentFiles);
}
}
});
//回到上一级目录的按钮
Button parent=(Button)findViewById(R.id.parent);
parent.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
if(!currentParent.getCanonicalPath().equals("mnt/sdcard"))
{
//获取上一级目录
currentParent=currentParent.getParentFile();
//列出当前目录下所有文件
currentFiles=currentParent.listFiles();
//再次更新ListView
inflateListView(currentFiles);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
private void inflateListView(File[] files) {
// TODO Auto-generated method stub
//创建一个List集合,List集合的元素师Map
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());
//添加List项
listItems.add(listItem);
}
//创建一个simpleadaptor, 使用ID为R.id.icon的组将来显示icon对应的值
SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.line,
new String[]{"icon","fileName"}, new int[]{R.id.icon,R.id.file_name});
//为listView设置Adapter
listView.setAdapter(simpleAdapter);
try
{
textView.setText("当前路径为: "+currentParent.getCanonicalPath());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
-----------------------------
<?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="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
/>
<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>