1.这几天在复习android基础,看到疯狂android讲义上有一个SD卡浏览器只用很少的代码就实现了功能,然后看了下他的思路,自己模仿了一下,由于自己的代码写的很繁琐,最后还是决定把书上的例子上传上来吧。先上效果图
代码链接:下载地址
2.分享一下自己的心得,自己以前在看到别人的例子时,总是大体看一下,以为自己会了,然后就开始敲,等到动手的时候,才发现自己没有完整的思路,所有,在动手之前,脑袋里应该完整的分析下例子,用到的方法等搞清楚之后再动手,这样在敲,解决问题,就是你的思路了,在对照一下,才会有所思路,不能当搬一辈子的砖啊。
3.具体的实现思路,手机获取SD卡上的目录之后,适用simpleAdapter来更新listView,当点击目录时,重新弄simpleAdapter,然后更新listview。
1.获取SD卡的目录:
File root = new File("/mnt/sdcard/");
String path = root.getCanonicalPath();
还有一种获得目录方法,但在我的真机android4.4上不适用,附上一个链接,比较完整的讲解了问题的原因链接
Environment.getExternalStorageDirectory();
完整代码:
public class SDFileExplorer extends Activity
{
ListView listView;
TextView textView;
// 记录当前的父文件夹
File currentParent;
// 记录当前路径下的所有文件的文件数组
File[] currentFiles;
String rootPath;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取列出全部文件的ListView
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();
try {
rootPath = currentParent.getCanonicalPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 使用当前目录下的全部文件、文件夹来填充ListView
inflateListView(currentFiles);
}
// 为ListView的列表项的单击事件绑定监听器
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// 用户单击了文件,直接返回,不做任何处理
if (currentFiles[position].isFile()) return;
// 获取用户点击的文件夹下的所有文件
File[] tmp = currentFiles[position].listFiles();
if (tmp == null || tmp.length == 0)
{
Toast.makeText(SDFileExplorer.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 source)
{
try
{
if (!currentParent.getCanonicalPath()
.equals(rootPath))
{
// 获取上一级目录
currentParent = currentParent.getParentFile();
// 列出当前目录下所有文件
currentFiles = currentParent.listFiles();
// 再次更新ListView
inflateListView(currentFiles);
}else{
Toast.makeText(getApplicationContext(), "已经是根目录,不能返回", Toast.LENGTH_SHORT).show();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
private void inflateListView(File[] files) //①
{
// 创建一个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>();
// 如果当前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 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 (IOException e)
{
e.printStackTrace();
}
}