android Listview 中顯示/mnt下檔案和資料夾
阿新 • • 發佈:2019-01-31
ListView中顯示/mnt下的檔案和資料夾,如果是檔案選擇外部程式開啟,是資料夾則顯示該目錄下的子檔案和子資料夾
程式碼如下:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view_demo); lv = (ListView) this.findViewById(R.id.lv); File file = Environment.getExternalStorageDirectory(); file = file.getParentFile(); bindListView(file, lv); }
bindListView方法:
getData方法://繫結listview public void bindListView(File file, ListView lv) { File[] files = file.listFiles(); this.setTitle(file.getPath()); SimpleAdapter adapter = new SimpleAdapter(this, getData(files), R.layout.list_layout, new String[] { "fileName" }, new int[] { R.id.tvFileName }); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub ListView lv = (ListView) arg0; Map<String, Object> map = (HashMap<String, Object>) lv .getItemAtPosition(arg2); File file = (File) map.get("fullPath"); // displayToast(file.getName()); if (file.isDirectory()) { bindListView(file, lv); }else if(file.isFile()){ openFile(file); } } }); }
public List<Map<String, Object>> getData(File[] files) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (File file : files) { Map<String, Object> map = new HashMap<String, Object>(); map.put("fileName", file.getName()); map.put("fullPath", file); list.add(map); } return list; }
openFile方法:
public void openFile(File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = getMIMEType(file);
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
}
getMIMEType方法:
public String getMIMEType(File file) {
String type="*/*";
String fName = file.getName();
//獲取字尾名前的分隔符"."在fName中的位置。
int dotIndex = fName.lastIndexOf(".");
if(dotIndex < 0){
return type;
}
/* 獲取檔案的字尾名*/
String end=fName.substring(dotIndex,fName.length()).toLowerCase();
if(end==""){
return type;
}
//在MIME和檔案型別的匹配表中找到對應的MIME型別。
for(int i=0;i<MIME_MapTable.length;i++){ //MIME_MapTable??在這裡你一定有疑問,這個MIME_MapTable是什麼?
if(end.equals(MIME_MapTable[i][0]))
type = MIME_MapTable[i][1];
}
return type;
}
MIME_MapTable陣列:
private final String[][] MIME_MapTable={
//{字尾名,MIME型別}
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop", "text/plain"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"},
{"", "*/*"}
};
返回上一層:
重寫onKeyDown方法:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
File file = new File(this.getTitle().toString());
Log.i(Tag, file.getPath());
Log.i(Tag,Environment.getExternalStorageDirectory()
.getParentFile().getPath());
if (file.equals(Environment.getExternalStorageDirectory().getParentFile())) {
return super.onKeyDown(keyCode, event);
} else {
bindListView(file.getParentFile(), lv);
return false;
}
} else {
return super.onKeyDown(keyCode, event);
}
}
用到的佈局檔案:
activity_list_view_demo.xml
<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="${relativePackage}.${activityClass}" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
list_layout.xml(控制Listview中每一行顯示的佈局檔案)<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvFileName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:scrollHorizontally="false"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>