SD卡:
public void
initData(){
File root = new File("/mnt/sdcard");
if (root.exists())
{
currentParent = root ;
currentFiles = root.listFiles();
inflateListView(currentFiles);
}
videoListView2.setOnItemClickListener(new
AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position,long
id)
{
if (currentFiles[position].isFile())
{
String filepath = currentFiles[position].getAbsolutePath();
Intent intent = new Intent();
intent.putExtra("filepath",filepath);
intent.setClass($.getContext(), PlayActivity.class);
startActivity(intent);
}
File[] tmp = currentFiles[position].listFiles();
if (tmp == null
|| tmp.length ==
0)
{
// $.toast("当前路径不可访问或该路径下没有文件",0);
}
else
{
currentParent =
currentFiles[position];
currentFiles = tmp ;
inflateListView(currentFiles);
}
}
});
btn_parent.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 adapter = new SimpleAdapter($.getContext(),listItems, R.layout.line,
new String[]{"icon","fileName"},
new int[]{R.id.icon,R.id.file_name});
videoListView2.setAdapter(adapter);
try
{
tv_path2.setText("当前路径:"+currentParent.getCanonicalPath());
$.toast(tv_path2.getText().toString(),0);
if(tv_path2.getText().toString().equals("当前路径:"+"/")){
btn_parent.setClickable(false);
btn_parent.setText("到达最顶级");
}else {
btn_parent.setClickable(true);
btn_parent.setText("返回上一级");
}
} catch (IOException e)
{
e.printStackTrace();
}
}