Android gets the file and cache path of the current program!

Source: Internet
Author: User

The main interface is to display two buttons to go to the file and cache directories respectively:

 

 

The program architecture is as follows:

 

 

Let's take a look at the main program code:

 

Public class filecacheactivity extends activity implements onclicklistener <br/>{< br/> private button mybutton1; <br/> private button mybutton2; <br/> private file cachedir; <br/> private file filedir; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> mybutton1 = (button) findviewbyid (R. id. mybutton1); <br/> mybutton2 = (button) findviewbyid (R. id. mybutton2); </P> <p> mybutton1.setonclicklistener (this); <br/> mybutton2.setonclicklistener (this ); </P> <p>/** <br/> * obtain the current cache directory <br/> */<br/> cachedir = This. getcachedir (); </P> <p>/** <br/> * obtain the current file directory <br/> */<br/> filedir = This. getfilesdir (); <br/>}</P> <p>/* Call ex05_16_1 and pass the path to */<br/> private void showactivity (string path) <br/>{< br/> intent = new intent (); <br/> intent. setclass (filecacheactivity. this, filecachelistactivity. class); </P> <p> bundle = new bundle (); <br/> bundle. putstring ("path", PATH); <br/> intent. putextras (bundle); </P> <p> startactivity (intent ); <br/>}</P> <p> @ override <br/> Public void onclick (view V) <br/> {<br/> // todo auto-generated method stub <br/> int viewid = v. GETID (); <br/> switch (viewid) <br/>{< br/> case R. id. mybutton1: <br/>{< br/> string Path = filedir. getparent () + file. separator <br/> + filedir. getname (); <br/> showactivity (PATH); <br/> break; <br/>}< br/> case R. id. mybutton2: <br/>{< br/> string Path = cachedir. getparent () + file. separator <br/> + cachedir. getname (); <br/> showactivity (PATH); <br/> break; <br/>}< br/> default: <br/> break; <br/>}</P> <p>}

 

Then the code of the listview is displayed:

 

Public class filecachelistactivity extends activity implements <br/> onitemclicklistener <br/>{< br/> private list <string> items = NULL; <br/> private string path; <br/> private listview; </P> <p>/** called when the activity is first created. */<br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. mylist); </P> <P> listview = (listview) findviewbyid (R. id. list); <br/> listview. setonitemclicklistener (this); <br/>/** <br/> * Get bundle <br/> */<br/> bundle bunde = This. getintent (). getextras (); </P> <p>/** <br/> * obtain the data imported into the bundle on the previous page. <br/> */<br/> Path = bunde. getstring ("path"); </P> <p> This. settitle (PATH ); </P> <p>/** <br/> * use a new file directory to display all files in the directory. <br/> */<br/> File = new file (PATH ); <br/> Fill (file. list Files (); // list all files under this path <br/>}</P> <p> private void fill (file [] files) <br/>{< br/> items = new arraylist <string> (); <br/> If (Files = NULL) <br/>{< br/> return; <br/>}</P> <p>/* put the obtained file name into the arraylist */<br/> for (File file: files) <br/>{< br/> items. add (file. getname ()); <br/>}</P> <p>/* put arraylist into arrayadapter */<br/> arrayadapter <string> filelist = new arrayadapter <string> (this, <br/> Andro Id. r. layout. simple_list_item_1, items); <br/> listview. setadapter (filelist); <br/>}</P> <p> @ override <br/> Public void onitemclick (adapterview <?> L, view V, int position, long ID) <br/>{< br/> file = new file (path + file. separator + items. get (position); </P> <p> If (file. isdirectory () <br/>{< br/> Fill (file. listfiles (); <br/>}</P> <p >}< br/>

 

The last two XML files

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <button <br/> Android: id = "@ + ID/mybutton1" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/strlist1" <br/> </button> <br/> <button <br/> Android: id = "@ + ID/mybutton2" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/strlist2" <br/> </button> <br/> </linearlayout>

 

Mylist. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> <listview <br/> Android: Id = "@ + ID/List" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> </listview> <br/> </linearlayout>

 

Finished!

 

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.