Android app small instance-SD card file browser

Source: Internet
Author: User

 

Finally:

 

Implement the SD card file browser

Technical analysis:

The main controls used:Listview

How to fill in the listview control, you need to traverse all the files under the entire SD card. If you traverse a folder, first obtain all the files under the folder, then we can fill all the files in this listview, so we can repeat it.

How can I get the modification date of a folder or file?

The file class in Java only provides us with a public long lastmodified (): The long value of the last modification time of the file. It is used with the time point (January 1, January 1, 1970, 00:00:00 GMT) in milliseconds. If the file does not exist or an I/O error occurs, 0l is returned.

File myfile = new file ("d :\\ common android development icons"); long modtime = myfile. lastmodified (); simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); system. out. println (dateformat. format (new date (modtime )));

 

Implementation Code:

 

 

Package COM. jiahui. sdcardfileexplorer; import Java. io. file; import Java. text. dateformat; import Java. text. simpledateformat; import Java. util. arraylist; import Java. util. date; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistene R; import android. widget. button; import android. widget. listview; import android. widget. simpleadapter; import android. widget. textview; import android. widget. toast; public class sdcardfileexploreractivity extends activity {private textview tvpath; private listview lvfiles; private button btnparent; // record the current parent folder file currentparent; // record the file array file [] currentfiles; Public void oncreate (bundle savedinstan Cestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); lvfiles = (listview) This. findviewbyid (R. id. files); tvpath = (textview) This. findviewbyid (R. id. tvpath); btnparent = (button) This. findviewbyid (R. id. btnparent); // obtain the system's sdcard directory file root = new file ("/mnt/sdcard/"); // If the SD card exists (root. exists () {currentparent = root; currentfiles = root. listfiles (); // use all files and folders in the current directory to fill in listvie Winflatelistview (currentfiles);} lvfiles. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Adapterview, view, int position, long ID) {// if the user clicks a file, return directly without any processing if (currentfiles [position]. isfile () {// You can also customize the extension to open this file and other return;} // obtain all the files in the folder clicked by the user [] TEM = currentfiles [position]. listfiles (); If (TEM = NULL | TEM. length = 0) {toast. maketext (sdcardfileexploreractivity. this, "the current path is not accessible or there are no files under this path", toast. length_long ). show ();} else {// get the folder corresponding to the list item clicked by the user, set as the current parent folder currentparent = currentfiles [Position]; // save all files and folders in the current parent folder currentfiles = TEM; // update listviewinflatelistview (currentfiles) again );}}}); // obtain the btnparent of the upper-level directory. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {try {If (! Currentparent. getCanonicalPath (). equals ("/mnt/sdcard") {// obtain the currentparent = currentparent. getparentfile (); // list all files in the current directory currentfiles = currentparent. listfiles (); // update listviewinflatelistview (currentfiles) again;} catch (exception e) {// todo: handle exception }}});} /*** fill listview ** @ Param files */private void inflatelistview (file [] files) {list <Map <string, object> listitems = new arraylist <Map <string, Object> (); For (INT I = 0; I <files. length; I ++) {Map <string, Object> listitem = new hashmap <string, Object> (); If (files [I]. isdirectory () {// if it is a folder, the picture displayed is the folder image listitem. put ("icon", R. drawable. folder);} else {listitem. put ("icon", R. drawable. file);} // Add a file name listitem. put ("FILENAME", files [I]. getname (); file myfile = new file (files [I]. getname (); // get the last modification date of the object long modtime = myfile. lastmodified (); simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); system. out. println (dateformat. format (new date (modtime); // Add a Last modified Date listitem. put ("modify", "modification date:" + dateformat. format (new date (modtime); listitems. add (listitem);} // defines a simpleadaptersimpleadapter adapter = new simpleadapter (sdcardfileexploreractivity. this, listitems, R. layout. list_item, new string [] {"FILENAME", "icon", "modify"}, new int [] {R. id. file_name, R. id. icon, R. id. file_modify}); // fill in the dataset lvfiles. setadapter (adapter); try {tvpath. settext ("current path:" + currentparent. getCanonicalPath ();} catch (exception e) {e. printstacktrace ();}}}

 

If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524

Source code download: http://download.csdn.net/download/jiahui524/3800378

 

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.