First look at the effect chart:
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;
/** * Tool class for obtaining folders and files for mobile phones, if permission allows, to obtain a list of files on any path on the phone * Getfilesutils uses a lazy single case mode, thread safety * @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 () {}/** * get getfilesutils instance * @return getfilesutils **/public static synchronized GETF
Ilesutils getinstance () {if (gfu==null) {gfu=new getfilesutils ();
return gfu; /** * Get file list under the path folder * @see #getSonNode (String) * @param the folder on the path cell * @return The list of files in the path folder Information, the information is stored in the map, and the map key is listed as follows: <br/> * file_info_name:string file name <br/> * file_info_isfolder:b Oolean whether <br/> * file_info_type:string file suffix <br/> * File_info_num_sondirs:int subfolder for folder Number of <br/> * file_info_num_sonfiles:int files <br/> * file_info_path:string file absolute path <b
R/> **/public list<map<string, object>> getsonnode (File path) {if (Path.isdirectory ()) {
List<map<string, object>> list=new arraylist<map<string,object>> ();
File[] Files=path.listfiles ();
if (files!=null) { for (int i=0;i<files.length;i++) {map<string, object> fileinfo=new hashmap<string, Object> ();
Fileinfo.put (File_info_name, Files[i].getname ());
if (Files[i].isdirectory ()) {Fileinfo.put (File_info_isfolder, true);
File[] Bfiles=files[i].listfiles ();
if (bfiles==null) {fileinfo.put (file_info_num_sondirs, 0);
Fileinfo.put (file_info_num_sonfiles, 0);
}else{int getnumofdir=0;
for (int j=0;j<bfiles.length;j++) {if (Bfiles[j].isdirectory ()) {getnumofdir++;
} fileinfo.put (File_info_num_sondirs, Getnumofdir);
Fileinfo.put (File_info_num_sonfiles, Bfiles.length-getnumofdir);
} fileinfo.put (File_info_type, File_type_folder);
}else{Fileinfo.put (File_info_isfolder, false); FileinfO.put (file_info_num_sondirs, 0);
Fileinfo.put (file_info_num_sonfiles, 0);
Fileinfo.put (File_info_type, Getfiletype (Files[i].getname ()));
} fileinfo.put (File_info_path, Files[i].getabsolutefile ());
List.add (FileInfo);
} return list;
}else{return null;
}}else{return null; /** * Get file list under Pathstr folder * @see #getSonNode (file) * @param the absolute path of the folder on the pathstr phone * @return Path The list of files in the Str folder, where the information is stored in the map, the map key is listed as follows: <br/> * file_info_name:string file name <br/> * File_inf O_isfolder:boolean is the suffix for the folder <br/> * file_info_type:string file <br/> * File_info_num_sondi Number of Rs:int subfolders <br/> * File_info_num_sonfiles:int sub-Files <br/> * file_info_path:string The absolute path to the file <br/> **/public list<map<string, object>> Getsonnode (String pathstr) {file path=nEW File (PATHSTR);
return Getsonnode (path); /** * Get file Path file or folder sibling file list * @see #getBrotherNode (String) * @param the folder on the path phone * @return path The list of files under the folder, the information is stored in the map, and the map key is listed as follows: <br/> * file_info_name:string file name <br/> * file_info_i Sfolder:boolean is the suffix for the folder <br/> * file_info_type:string file <br/> * file_info_num_sondirs : Int subfolder number <br/> * file_info_num_sonfiles:int <br/> * file_info_path:string files Absolute path <br/> **/public list<map<string, object>> getbrothernode (File path) {if (Path.getparen
Tfile ()!=null) {return Getsonnode (Path.getparentfile ());
}else{return null; /** * Get file Path file or folder's sibling file list * @see #getBrotherNode (File) * @param folder on the path phone * @return path The list of files under the folder, the information is stored in the map, and the map key is listed as follows: <br/> * file_info_name:string file name <br/> * file_iNfo_isfolder:boolean is the suffix for the folder <br/> * file_info_type:string file <br/> * File_info_num_son Number of Dirs:int subfolders <br/> * File_info_num_sonfiles:int sub-Files <br/> * File_info_path:strin Absolute path to G file <br/> **/public list<map<string, object>> Getbrothernode (String pathstr) {file P
Ath=new File (PATHSTR);
return Getbrothernode (path); /** * Gets the parent path of the file or folder * @param the files path file or folder * @return the parent path of string path **/public string getpa
RentPath (File path) {if (Path.getparentfile () ==null) {return null;
}else{return path.getparent (); /** * Gets the file or file's parent path * @param string pathstr file or folder path * @return The parent path of string pathstr **/public St
Ring Getparentpath (String pathstr) {file Path=new file (PATHSTR);
if (Path.getparentfile () ==null) {return null;
}else{return path.getparent (); }/** * Get SD CardAbsolute path * @return string if the SD card exists, return the absolute path of the SD card, otherwise return null **/public String Getsdpath () {string sdcard=environmen
T.getexternalstoragestate (); if (Sdcard.equals (environment.media_mounted)) {return environment.getexternalstoragedirectory (). GetAbsolutePath ()
;
}else{return null;
}/** * Get a basic path, general application create store application data can be used to * @return String if the SD card exists, return the absolute path of the SD card, if the SD card does not exist, return the absolute path of the Android data directory
**/public String Getbasepath () {string Basepath=getsdpath ();
if (basepath==null) {return environment.getdatadirectory (). GetAbsolutePath ();
}else{return basepath; /** * Gets the size of the file path * @return the size of the string path **/public string GetFileSize (File path) throws IO
exception{if (path.exists ()) {DecimalFormat df = new DecimalFormat ("#.00");
String sizestr= "";
FileInputStream fis=new FileInputStream (path);
Long size=fis.available ();
Fis.close (); if (size<1024) {sizestr=size+ "B";
}else if (size<1048576) {Sizestr=df.format (size/(double) 1024) + "KB";
}else if (size<1073741824) {Sizestr=df.format (size/(double) 1048576) + "MB";
}else{Sizestr=df.format (size/(double) 1073741824) + GB;
return sizestr;
}else{return null;
/** * Gets the size of the file Fpath * @return the size of the string path **/public string GetFileSize (string fpath) {
File Path=new file (Fpath);
if (path.exists ()) {DecimalFormat df = new DecimalFormat ("#.00");
String sizestr= "";
Long size=0;
try {fileinputstream fis = new FileInputStream (path);
Size=fis.available ();
Fis.close ();
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
Return "Unknown size";
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); Return "Unknown size";
} if (size<1024) {sizestr=size+ "B";
}else if (size<1048576) {Sizestr=df.format (size/(double) 1024) + "KB";
}else if (size<1073741824) {Sizestr=df.format (size/(double) 1048576) + "MB";
}else{Sizestr=df.format (size/(double) 1073741824) + GB;
return sizestr;
}else{return "Unknown size"; /** * Gets the type of file filename based on suffix * @return string file type **/public string Getfiletype (string Filenam
e) {if (filename!= "" &&filename.length () >3) {int Dot=filename.lastindexof (".");
if (dot>0) {return filename.substring (dot+1);
}else{return "";
} return ""; Public comparator<map<string, Object>> Defaultorder () {final String orderby0=file_info_is
FOLDER;
Final String Orderby1=file_info_type;
Final String Orderby2=file_info_name; comparator<Map<string, object>> order=new comparator<map<string,object>> () {@Override public in
T compare (map<string, object> lhs, map<string, object> RHS) {//TODO auto-generated method stub
int Left0=lhs.get (orderBy0). Equals (true)? 0:1;
int Right0=rhs.get (orderBy0). Equals (true)? 0:1;
if (left0==right0) {String left1=lhs.get (orderBy1). toString ();
String Right1=rhs.get (orderBy1). toString ();
if (Left1.compareto (right1) ==0) {String left2=lhs.get (orderBy2). toString ();
String Right2=rhs.get (orderBy2). toString ();
Return Left2.compareto (right2);
}else{return Left1.compareto (right1);
}}else{return left0-right0;
}
}
};
return order; }
}
Usage: list<map<string, object>> list=getfilesutils.getinstance (). Getsonnode (file),//or other method
Use example:
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 folder
Lv;
Private TextView foldernowtv;
Private Simpleadapter Sadapter;
Private list<map<string, object>> 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 ("local file");
folderlv= (ListView) Findviewbyid (r.id.folder_list);
foldernowtv= (TextView) Findviewbyid (R.id.folder_now);
Foldernowtv.settext (Basefile);
Foldernowtv.setonclicklistener (this);
Alist=new arraylist<map<string,object>> (); 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 (StringFile) throws ioexception{list<map<string, Object>> list=getfilesutils.getinstance (). GetSonNode (file);
if (list!=null) {collections.sort (list, getfilesutils.getinstance (). Defaultorder ());
Alist.clear ();
For (map<string, object> map:list) {string filetype= (String) map.get (Getfilesutils.file_info_type);
Map<string,object> gmap=new hashmap<string, object> ();
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) + "folders and" + Map.get (getfilesutils.file_info_num
_sonfiles) + "files");
}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", "File Size:" +getfilesutils.getinstance (). GetFileSize (Map.get (getfilesutils.file_info_path). ToStri
Ng ()));
} 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<?> parent, view view, int position, long ID) {/ /TODO auto-generated Method stub try {if (Alist.get (position). Get ("Fisdir"). Equals (True) {Loadfolde
Rlist (Alist.get (position). Get ("Fpath"). toString ());
}else{Toast.maketext (This, "Here is the file, handler to add", Toast.length_short). Show ();
The 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.geti
Nstance (). Getparentpath (Foldernowtv.gettext (). toString ());
if (folder==null) {Toast.maketext (this, "No parent directory, pending", Toast.length_short). Show ();
}else{loadfolderlist (folder);
The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }
}
}
}
The layout file for the page is:
<?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 " android:background= "@color/frame_main_bg" > <include layout= "@layout/mypage_title_folder"/> <TextView Android:layout_width= "Match_parent" android:layout_height= "40DP" android:textcolor= "#FFFFFF" android:texts Ize= "16sp" android:gravity= "center_vertical" android:ellipsize= "Start" android:singleline= "true" Andro id:drawableleft= "@drawable/folder_backupimg" android:paddingleft= "10DP" android:drawablepadding= "10DP" Andr Oid:id= "@+id/folder_now" android:background= "@color/frame_title_bg_clk_color"/> <listview android:layout_wi Dth= "Match_parent" android:layout_height= "wrap_content" android:id= "@+id/folder_list" android:dividerHeight = "1DP" android:divider= "@coLor/folder_list_divider "> </ListView> </LinearLayout>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.