先看看效果图:
package wuwang.tools.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Environment; /** * 用于获取手机的文件夹及文件的工具类,如果权限允许,可以获取手机上任意路径的文件列表 * GetFilesUtils使用的是懒汉式单例模式,线程安全 *@author wuwang *@since 2014.11 */ public class GetFilesUtils { public static final String FILE_TYPE_FOLDER="wFl2d"; public static final String FILE_INFO_NAME="fName"; public static final String FILE_INFO_ISFOLDER="fIsDir"; public static final String FILE_INFO_TYPE="fFileType"; public static final String FILE_INFO_NUM_SOnDIRS="fSonDirs"; public static final String FILE_INFO_NUM_SOnFILES="fSonFiles"; public static final String FILE_INFO_PATH="fPath"; private static GetFilesUtils gfu; private GetFilesUtils(){ } /** * 获取GetFilesUtils实例 * @return GetFilesUtils **/ public static synchronized GetFilesUtils getInstance(){ if(gfu==null){ gfu=new GetFilesUtils(); } return gfu; } /** * 获取文件path文件夹下的文件列表 * @see #getSonNode(String) * @param path 手机上的文件夹 * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:
* FILE_INFO_NAME : String 文件名称
* FILE_INFO_ISFOLDER: boolean 是否为文件夹
* FILE_INFO_TYPE: string 文件的后缀
* FILE_INFO_NUM_SONDIRS : int 子文件夹个数
* FILE_INFO_NUM_SONFILES: int 子文件个数
* FILE_INFO_PATH : String 文件的绝对路径
**/ public List
使用方法:List
使用示例:
package wuwang.mypage.activity; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import wuwang.ebookworm.R; import wuwang.tools.utils.GetFilesUtils; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class FolderActivity extends Activity implements OnItemClickListener,OnClickListener { private ListView folderLv; private TextView foldernowTv; private SimpleAdapter sAdapter; private List> aList; private String baseFile; private TextView titleTv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.mypage_folder); baseFile=GetFilesUtils.getInstance().getBasePath(); titleTv=(TextView) findViewById(R.id.mtitle); titleTv.setText("本地文件"); folderLv=(ListView) findViewById(R.id.folder_list); foldernowTv=(TextView) findViewById(R.id.folder_now); foldernowTv.setText(baseFile); foldernowTv.setOnClickListener(this); aList=new ArrayList >(); sAdapter=new SimpleAdapter(this, aList,R.layout.listitem_folder, new String[]{"fImg","fName","fInfo"}, new int[]{R.id.folder_img,R.id.folder_name,R.id.folder_info}); folderLv.setAdapter(sAdapter); folderLv.setOnItemClickListener(this); try { loadFolderList(baseFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void loadFolderList(String file) throws IOException{ List > list=GetFilesUtils.getInstance().getSonNode(file); if(list!=null){ Collections.sort(list, GetFilesUtils.getInstance().defaultOrder()); aList.clear(); for(Map map:list){ String fileType=(String) map.get(GetFilesUtils.FILE_INFO_TYPE); Map gMap=new HashMap (); if(map.get(GetFilesUtils.FILE_INFO_ISFOLDER).equals(true)){ gMap.put("fIsDir", true); gMap.put("fImg",R.drawable.filetype_folder ); gMap.put("fInfo", map.get(GetFilesUtils.FILE_INFO_NUM_SONDIRS)+"个文件夹和"+ map.get(GetFilesUtils.FILE_INFO_NUM_SONFILES)+"个文件"); }else{ gMap.put("fIsDir", false); if(fileType.equals("txt")||fileType.equals("text")){ gMap.put("fImg", R.drawable.filetype_text); }else{ gMap.put("fImg", R.drawable.filetype_unknow); } gMap.put("fInfo","文件大小:"+GetFilesUtils.getInstance().getFileSize(map.get(GetFilesUtils.FILE_INFO_PATH).toString())); } gMap.put("fName", map.get(GetFilesUtils.FILE_INFO_NAME)); gMap.put("fPath", map.get(GetFilesUtils.FILE_INFO_PATH)); aList.add(gMap); } }else{ aList.clear(); } sAdapter.notifyDataSetChanged(); foldernowTv.setText(file); } @Override public void onItemClick(AdapterView<&#63;> parent, View view, int position, long id) { // TODO Auto-generated method stub try { if(aList.get(position).get("fIsDir").equals(true)){ loadFolderList(aList.get(position).get("fPath").toString()); }else{ Toast.makeText(this, "这是文件,处理程序待添加", Toast.LENGTH_SHORT).show(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onClick(View v) { // TODO Auto-generated method stub if(v.getId()==R.id.folder_now){ try { String folder=GetFilesUtils.getInstance().getParentPath(foldernowTv.getText().toString()); if(folder==null){ Toast.makeText(this, "无父目录,待处理", Toast.LENGTH_SHORT).show(); }else{ loadFolderList(folder); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
页面的布局文件为:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。