Android Simple file Browser source code (GO)

Source: Internet
Author: User

Android Simple file Browser source code (GO)

Activity_main. xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Wrap_content "    android:orientation= "vertical"    android:layout_gravity= "center_horizontal"    tools:context= ". Mainactivity ">    <textview        android:id=" @+id/txt1 "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"/>   <imagebutton        android:id= "@+id/imagebt1"       android: Layout_width= "Wrap_content"       android:layout_height= "wrap_content"       android:src= "@drawable/Home"/>    <listview        android:id= "@+id/listfile"        android:layout_width= "Wrap_content"        android: layout_height= "Wrap_content" >    </ListView></LinearLayout>

Fileimageandtext.xml

<?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:o rientation= "Horizontal" >    <imageview        android:id= "@+id/images" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "/>    <textview        android:id=" @+id/txtview        " Android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content"/></linearlayout>

Activity_main.java

Package Com.android.xiong.sdfilelook;import Java.io.file;import Java.util.arraylist;import java.util.HashMap; Import Java.util.list;import java.util.map;import Android.app.activity;import Android.os.bundle;import Android.os.environment;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener ; Import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.imagebutton;import Android.widget.imageview;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.textview;public class Mainactivity extends Activity {private ListView listfile;//Current file directory private String currentpath;private TextView txt1;private ImageView images;private TextView Textview;private ImageButton imagebt1;private int[] img = {r.drawable.file, r.drawable.folder, r.drawable.home}; Private file[] files;private simpleadapter simple; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstanceState); Setcontentview (r.layout.activity_main); listfile = (ListView) Findviewbyid (r.id.listfile); txt1 = (TextView) Findviewbyid (R.ID.TXT1); imagebt1 = (ImageButton) Findviewbyid (R.ID.IMAGEBT1); Init ( Environment.getexternalstoragedirectory ()); Listfile.setonitemclicklistener (new Onitemclicklistener () {@ overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2,long arg3) {//TODO auto-generated method s tub//gets the name of the file or folder that you clicked String folder = ((TextView) Arg1.findviewbyid (R.id.txtview)). GetText (). toString (); try {File Filef = new File (Currentpath + '/' + folder); init (Filef);} catch (Exception e) {e.printstacktrace ();}}}); /back to root directory Imagebt1.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {init ( Environment.getexternalstoragedirectory ());});} Interface initialization public void Init (File f) {if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {// Get all filenames under SDcard directory files = f.listfiles (); if (!files.equals (null)) {Currentpath=f.getpath (); txt1.settext("Current directory is:" +f.getpath ()); list<map<string, object>> list = new arraylist<map<string, object>> (); for (int i = 0; i < files . length; i++) {map<string, object> maps = new hashmap<string, object> (); if (Files[i].isfile ()) maps.put ("image", img[ 0]); Elsemaps.put ("image", img[1]); Maps.put ("Filenames", Files[i].getname ()); List.add (maps); Simple = new Simpleadapter (this, list,r.layout.fileimageandtext, new string[] {"Image", "filenames"}, new int[] {r.id.im Ages,r.id.txtview}); Listfile.setadapter (simple);}} else {System.out.println ("The file is Empty");}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Android Simple file Browser source code (GO)

Related Article

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.