Android File Browser Summary

Source: Internet
Author: User

The file browser is used to read all files and folders in the Android system. Specify the following:

    1. The current file path is displayed at the top. If it is the root directory, the "root directory" is displayed;

    2. The second line is to return to the previous Level button. If it is currently in the root directory, the row is not displayed;

    3. The file display uses the ListView control, all files are loaded once, if the current folder, you can click, go to the next level of the directory, if the file, click, the default is to select the file. Returns the filename and closes the file browser.

Cases:

File Explorer Activity

public class filemanageractivity extends activity {private textview  mcurrentpath;private textview mreturn;private listview mlist;private view  mpathline;private string mreturnpath = null;private filemanageradapter adapter; private arraylist<map<string, object>> infos = null; @Overrideprotected  void oncreate (bundle savedinstancestate)  {super.oncreate (savedInstanceState); Setcontentview (r.layout.file_list); Initview ();} Private void initview ()  {mCurrentPath =  (TextView)  findviewbyid (R.id.file_path); Mpathline = findviewbyid (R.id.file_path_line);mreturn =  (TextView)  findviewbyid ( R.id.file_return);mlist =  (ListView)  findviewbyid (r.id.file_list); Mlist.setonitemclicklistener ( Clicklistener); Mreturn.setonclicklistener (New onclicklistener ()  {@Overridepublic  void oNclick (VIEW&NBSP;V)  {string returnstr = mreturn.gettext (). toString ();if  ( Mreturnpath.length ()  > 0 && returnstr.equals ("Return to upper Level"))  {initlist ( Mreturnpath);  }}); Initlist ("/");    //initialization starts at the root directory}private void initlist (String  path)  {file file = new file (path); File[] filelist = file.listfiles ();infos = new arraylist<map<string,  Object>> (); Map<string, object> item = new hashmap<string, object> ();D rawable  drawable;if  (Path.equals ("/"))  {   //if current is the root, return to the previous Level button, do not display drawable =  Getresources (). getdrawable (R.drawable.versionup);d rawable.setbounds (0, 0, drawable.getminimumwidth () , Drawable.getminimumheight ()); mcurrentpath.setcompounddrawablepadding (); Mcurrentpath.setcompounddrawables ( Drawable, null, null, null); Mcurrentpath.settext ("root directory columnMreturnh.setvisibility (View.gone); mpathline.setvisibility (View.gone);}  else {drawable = getresources (). getdrawable (R.drawable.versionup);d rawable.setbounds (0,  0, drawable.getminimumwidth (), Drawable.getminimumheight ()); Mreturn.setcompounddrawables (drawable,  null, null, null); Mreturn.settext ("Return to the upper level"); Mreturnpath = file.getparent ();   //Save the top-level path mcurrentpath.setvisibility (view.visible) of the level directory; mpathline.setvisibility (view.visible); Mcurrentpath.settext (File.getpath ());} try {for  (int i = 0; i < filelist.length; i++)  {item  = new HashMap<String, Object> (); file fileitem = filelist[i];if  (Fileitem.isdirectory ())  {  //if the current file is a folder, Set the icon for the folder Item.put ("icon",  r.drawable.icon_one);}  elseitem.put ("icon",  r.drawable.icon_two); Item.put ("Name",  fileitem.getname ()); Item.put ("Path" ,  fileitem.getabsolutepath ()); Infos.add (item);}}  catch  (exception e)  {e.printstacktrace ();} Adapter = new filemanageradapter (this); Adapter.setfilelistinfo (infos); MList.setAdapter (adapter) ;} Private onitemclicklistener clicklistener = new onitemclicklistener ()  {@ Overridepublic void onitemclick (adapterview<?> arg0, view arg1, int  POSITION,LONG&NBSP;ARG3)  {file file = new file (String)   (infos.get (position). Get ("path")); if  (File.isdirectory ())  {  //If you click on a folder, go to the next level directory string nextpath =  (String)   (Infos.get (position). Get ("path")); Initlist (Nextpath);}  else {            //If you click on the file, The filename is sent to the main interface of the calling file browser intent intent = new intent (); Intent.setclass (Filemanageractivity.this, A.class); Intent.putextra ("FileName", (String)   (infos.get (position). Get ("name")); Intent.putextra ("PAth ",  (String)   (infos.get (position). Get (" path ")); Setresult (result_ok, intent); Finish ();}}}; 

Adapter

of the file browser

public class filemanageradapter extends baseadapter{private context mcontext; private list<map<string, object>> list = new arraylist<map< String,object>> ();p ublic filemanageradapter (Context context)  {super (); mContext =  context;} @Overridepublic  int getcount ()  {return list.size ();} @Overridepublic  object getitem (int position)  {return position;} @Overridepublic  long getitemid (int arg0)  {return arg0;} @Overridepublic  view getview (int position, view convertview, viewgroup arg2 )  {filemangerholder holder;if (Null == convertview) {holder = new  Filemangerholder (); Convertview = layoutinflater.from (Mcontext). Inflate (R.layout.file_item, null );holder.icon =  (ImageView)  convertview.findviewbyid (R.id.file_item_icon);holder.name =  (TextView)  convertview.findviewbyid (r.id.file_item_name); Convertview.settag (holder);} else{holder =  (Filemangerholder)  convertview.gettag ();} Holder.icon.setImageResource ((Integer) (List.get (position). Get ("icon")); Holder.name.setText ((String) (List.get ( Position). Get ("name"))); Return convertview;} Public class filemangerholder{public imageview icon;public textview name;} Public void setfilelistinfo (List<map<string, object>> infos) {list.clear (); List.addall (infos); notifydatasetchanged ();}}

File_list.layout

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"      Android:layout_width= "Match_parent"     android:layout_height= "Match_parent"      android:background= "@color/black"     android:orientation= "vertical"  >     <textview        android:id= "@+id/file_path "        android:layout_width="      "Match_parent"     android:layout_height= "Wrap_content"          Android:layout_margin= "5DP"         android:textcolor= "@color/white"         android:textsize= "12SP"  />    < View        android:id= "@+id/file_path_line"          androId:layout_width= "Match_parent"         android:layout_height= "0.3DP"         android:background= "@color/gray_dark"  />     <textview        android:id= "@+id/file_return"          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"         android: Layout_margin= "5DP"         android:textcolor= "@color/white"          android:textsize= "12SP"           />    <view        android: Layout_width= "Match_parent"         android:layout_height= "0.3DP"          android:background= "@color/gray_dark"  />    <ListView         android:id= "@+id/file_list"          Android:layout_width= "match_parent"         android:layout_height= "wrap _content "        android:cachecolorhint=" @color/transparent "         android:divider= "@color/gray_dark"          android:dividerheight= "0.3DP"         android: Listselector= "@null"         android:scrollbars= "None"  /></ Linearlayout>

File_item.layout

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"      Android:layout_width= "Match_parent"     android:layout_height= "Match_parent"      android:gravity= "center_vertical"     android:orientation= "Horizontal"   >    <imageview        android:id= "@+id/file _item_icon "        android:layout_width=" Wrap_content "         android:layout_height= "Wrap_content"          android:layout_margin= "5DP"  />    <TextView         android:id= "@+id/file_item_name"          Android:layout_width= "0DP"         android:layout_height= "Wrap_content"      &nbsP;  android:layout_margin= "5DP"         android:layout_weight = "1"         android:textcolor= "@color/white"          android:textsize= "12SP"  /></LinearLayout>


Android File Browser Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.