Copy Phone folder Manager

Source: Internet
Author: User
Tags dateformat

Imitation Phone folder Manager, can open the display SD card under the file, you can go to the next level and return to the previous level.


Key code into the following:

Import Java.io.file;import java.io.ioexception;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 Com.stcyclub.zhimeng.R; Import Com.stcyclub.zhimeng.r.drawable;import Com.stcyclub.zhimeng.r.id;import com.stcyclub.zhimeng.r.layout; Import Android.os.bundle;import android.os.environment;import android.app.activity;import android.content.Intent; Import Android.util.log;import android.view.menu;import Android.view.view;import Android.view.window;import Android.widget.adapterview;import Android.widget.button;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.textview;import Android.widget.toast;import Android.widget.adapterview.onitemclicklistener;public class Locationfileliabraryactivity extends Activity {private TextView tvpath;private ListView lvfiles;private Button btnparent;//records the current parent folder file currentparent;// Records an array of files for all folders under the current path file[] currentfiles;String Sdcardpath; String Folderpath;public string Getsdpath () {File Sddir = Null;boolean sdcardexist = environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED); Determine if the SD card exists if (sdcardexist) {sddir = Environment.getexternalstoragedirectory ();//Gets the root directory}return sddir.tostring ();} @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature ( Window.feature_no_title); Setcontentview (r.layout.activity_sdcard_image);//bundle bundle = new Bundle ();//Bundle = This.getintent (). Getextras ();//if (bundle! = null) {//imageorresfile = Bundle.getboolean ("Imageorresfile");/} Lvfiles = (ListView) This.findviewbyid (r.id.listview); Tvpath = (TextView) This.findviewbyid (r.id.tvpath); btnparent = ( Button) This.findviewbyid (r.id.btnparent); Sdcardpath = Getsdpath (); folderpath = Sdcardpath + "/";//Gets the SDcard directory of the system file Root = new file (sdcardpath);//If the SD card exists, image Orresfile ture get Picture false Get file if (Root.exists ()) {//if (imageorresfile) {//curreNtparent = Root;//currentfiles = Root.listfiles ();////} else {currentparent = new File (folderpath); currentfiles = current Parent.listfiles ();//}//fills Listviewinflatelistview (currentfiles) with all files and folders in the current directory;} Lvfiles.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> Adapterview, View view,int position, long ID) {//If the user clicks the file, returns directly, does not do any processing if (Currentfiles[position].isfile ()) {// You can also customize the extension to open this file such as String name = Currentfiles[position].getname ();//LOG.D ("TAG", "file...name.0 .... +name"); int index= Name.lastindexof ("."); /LOG.D ("TAG", "index...name.0 ...." +index); if (index>0) {String suffix=name.substring (index+1, Name.length ()); LOG.D ("TAG", "suffix.0 ...." +suffix); Suffix.equals ("EXE") | | Suffix.equals ("Bat"))) {string imagePath = new String (); try {ImagePath = Currentparent.getcanonicalpath () + "/" + name;} CA TCH (IOException e) {e.printstacktrace ();} Intent Intent = new Intent ();//Intent.setclass (sdcardimageactivity.this,//chatactivity.class); BunDle bundle = new Bundle (); Bundle.putstring ("FilePath", ImagePath); Intent.putextras (bundle); LocationFileLiabraryActivity.this.setResult (10002, intent); LocationFileLiabraryActivity.this.finish (); Overridependingtransition (R.anim.in_from_left, r.anim.out_to_rigth);} Else{toast.maketext (locationfileliabraryactivity.this, "format not supported", Toast.length_long). Show ();}} Else{toast.maketext (locationfileliabraryactivity.this, "format not supported", Toast.length_long). Show (); return;} Get all files under the folder that the user clicked file[] tem = currentfiles[position].listfiles (); if (tem = = NULL | | tem.length = = 0) {Toast.maketext (Lo Cationfileliabraryactivity.this, "The current path is inaccessible or there is no file under this path," Toast.length_long). Show (); else {//Gets the folder for the list item that the user clicked, set to the current parent folder Currentparent = currentfiles[position];//Save All files and folders in the current parent folder Currentfiles = tem;// Update Listviewinflatelistview (currentfiles);}}); /Get previous level directory Btnparent.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {try {if (! Currentparent.getcanonicalpath (). Equals (Sdcardpath)) {//Get top level DirectoryCurrentparent = Currentparent.getparentfile ();//Lists all files under the current directory currentfiles = Currentparent.listfiles ();// Update Listviewinflatelistview (Currentfiles) again;}} catch (Exception e) {//Todo:handle Exception}}}); public void Btnclick (View v) {switch (V.getid ()) {case R.id.location_file_btn_back: LocationFileLiabraryActivity.this.finish (); Overridependingtransition (R.anim.in_from_left, r.anim.out_to_rigth); Break;default:break;}} 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<st Ring, object> (), if (Files[i].isdirectory ()) {///If it is a folder, the picture that is displayed is a picture of the folder Listitem.put ("icon", R.drawable.folder_ picture);} else {String name=files[i].getname (); int Index=name.lastindexof ("."); if (index>0) {String suffix=name.substring (index+1, Name.length ()); if (suffix.equals ("jpg") | | suffix.equals ("BMP" )|| Suffix.equals ("JPEG") | | Suffix.equaLS ("png")) {listitem.put ("icon", r.drawable.picture);} else if (suffix.equals ("avi") | | Suffix.equals ("mkv") | | Suffix.equals ("mp4") | | Suffix.equals ("flv") | | Suffix.equals ("3gp") | | Suffix.equals ("RMVB")) {listitem.put ("icon", r.drawable.video_picture);} else if (suffix.equals ("apk")) {listitem.put ("icon", r.drawable.apk_picture);} else if (suffix.equals ("xls") | | Suffix.equals ("Doc") | | Suffix.equals ("TXT")) {listitem.put ("icon", r.drawable.word_picture);} else if (suffix.equals ("Zip") | | Suffix.equals ("rar")) {listitem.put ("icon", r.drawable.zip_picture);} else if (Suffix.equals ("MP3")) {listitem.put ("icon", r.drawable.music_picture);} Else{listitem.put ("icon", R.drawable.unknow_picture);}} Else{listitem.put ("icon", R.drawable.unknow_picture);}} Add a file name Listitem.put ("filename", files[i].getname ()); File MyFile = new file (Files[i].getname ());//Gets the last modified date of the file 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",//"Modified Date:" + Dateformat.format (new Date (modtime))); Listitems.add (ListItem);} Define a Simpleadaptersimpleadapter adapter = new Simpleadapter (Locationfileliabraryactivity.this,listitems, R.layout.sdcard_image_list_item, new string[] {"filename", "icon", "Modify"}, new int[] {r.id.file_name, R.id.icon, r.id . file_modify});//Fill Data Set Lvfiles.setadapter (adapter); try {tvpath.settext ("Current path is:" + currentparent.getcanonicalpath () );} catch (Exception e) {e.printstacktrace ();}} @Overridepublic void OnStart () {Super.onstart ();} @Overridepublic void Onresume () {Super.onresume ();} @Overridepublic void OnPause () {super.onpause ();} @Overridepublic void OnStop () {super.onstop ();} @Overridepublic void OnDestroy () {Super.ondestroy ();} @Overridepublic void Onrestart () {Super.onrestart ();}}

XML layout:

<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=" Match_parent "Android:o rientation= "vertical" ><relativelayout android:id= "@+id/rl_layout" android:layout_width= "Fill_parent" android:layout_height= "@dimen/activity_main_tab_title_height" android:background= "@color/black_top" Android: Layout_alignparenttop= "true" android:gravity= "center_vertical" > <imagebutton android:id= "@+id/location _file_btn_back "android:layout_width=" @dimen/activity_btn_height_login_and_register "Android:layout_ height= "Fill_parent" android:layout_centervertical= "true" android:background= "@drawable/btn_click_go        Back "android:src=" @drawable/goback_left "android:onclick=" Btnclick "/> <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_Content "android:text=" file Select "Android:layout_centerinparent=" true "android:textsize=" @dimen/activity_ textsize20_4.0 "android:textcolor=" #ffffff "/> <button android:id=" @+id/btnparent "and            Roid:layout_width= "60DP" android:layout_height= "30DP" android:layout_alignparentright= "true" android:layout_marginright= "10DP" android:layout_centerinparent= "true" android:background= "@draw Able/btn_click1 "android:textcolor=" @color/login_btn_text_color "android:text=" upper level "/></relativelayo ut> <textview android:id= "@+id/tvpath" android:layout_width= "Match_parent" android:layout _height= "Wrap_content" android:background= "@color/black20" android:textcolor= "@color/black" android:t Extsize= "15SP" android:text= "file path"/><listview android:id= "@+id/listview" android:layout_width = "Fill_parent" Android:layout_height= "Fill_parent" ></ListView> </LinearLayout> 


Copy Phone folder Manager

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.