Implementation of file selector in Android

Source: Internet
Author: User


Reprinted please indicate the source: http://blog.csdn.net/qinjuning

 


Today, we will share with you the role of the file selector, specifically, to obtain the path of the file/folder selected by the user on the SD card, similar to in C #.

Openfiledialog control (do not forget the all-in-one development of C ). Functions are easy to implement, mainly to help you save development time.

 

A widely spread product on the internet is as follows <[Android instance] File Selector>, This article is also modified based on the finished product above to make it easier

Understanding, higher efficiency. In addition, the main features include:

1. We listened to the event when the user presses the back key to make it return to the previous directory;

2. Special processing is made for different file types (File vs folder, target file vs other files.

 

Knowledge Point 1. Use of the file class

The main functions of the file selector are: browsing files \ folders, file types, etc. They are all implemented through the Java File class.

For the usage of the file class, refer to the following blog:

<File class from Java documentation>

<Java File class>


Knowledge Point 2. Call method description

The startactivityforresult () method is used to initiate a call and the onactivityresult () method to receive the callback information.

 

As follows:




Nothing else can be said. Let's take a look at the code comments ~~ So easy --.


Filechooseractivity. JavaClass for implementing file selection.

Public class copyoffilechooseractivity extends activity {private string msdcardrootpath; // sdcard root path private string mlastfilepath; // The currently displayed path private arraylist <fileinfo> mfilelists; private filechooadapter sermadatper; // configure the adapter private void setgridviewadapter (string filepath) {updatefileitems (filepath); madatper = new filechooseradapter (this, mfilelists); mgridview. setadapter (madatper);} // update data by path and pass Adatper data changes private void updatefileitems (string filepath) {mlastfilepath = filepath; mtvpath. settext (mlastfilepath); If (mfilelists = NULL) mfilelists = new arraylist <fileinfo> (); If (! Mfilelists. isempty () mfilelists. clear (); file [] files = folderscan (filepath); If (Files = NULL) return; For (INT I = 0; I <files. length; I ++) {If (files [I]. ishidden () // hidden file continue is not displayed; string fileabsolutepath = files [I]. getabsolutepath (); string filename = files [I]. getname (); Boolean isdirectory = false; If (files [I]. isdirectory () {isdirectory = true;} fileinfo = new fileinfo (fileabsolu Tepath, filename, isdirectory); // Add to mfilelists. Add (fileinfo);} // when first enter, the object of madatper don't initializedif (madatper! = NULL) madatper. notifydatasetchanged (); // refresh} // obtain all the files in the current path. Private file [] folderscan (string path) {file = new file (PATH ); file [] files = file. listfiles (); Return files;} private adapterview. onitemclicklistener mitemclicklistener = new onitemclicklistener () {public void onitemclick (adapterview <?> Adapterview, view, int position, long ID) {fileinfo = (fileinfo) (filechooseradapter) adapterview. getadapter ()). getitem (position); If (fileinfo. isdirectory () // click the folder to display all files in the folder updatefileitems (fileinfo. getfilepath (); else if (fileinfo. ispptfile () {// indicates the pptfile, which notifies the caller of intent = new intent (); intent. putextra (extra_file_chooser, fileinfo. getfilepath (); setresult (result_ OK, Intent); finish ();} else {// other files ..... toast (gettext (R. string. open_file_error_format) ;}}; public Boolean onkeydown (INT keycode, keyevent event) {If (event. getaction () = keyevent. action_down & event. getkeycode () = keyevent. keycode_back) {backprocess (); Return true;} return Super. onkeydown (keycode, event);} // return the Operation Public void backprocess () in the previous directory {// determine whether the current path is an sdcard path. If not, return to the previous layer. If (! Mlastfilepath. equals (msdcardrootpath) {file thisfile = new file (mlastfilepath); string parentfilepath = thisfile. getparent (); updatefileitems (parentfilepath);} else {// is the sdcard path, which ends setresult (result_canceled); finish ();}}}

The interface is still ugly. You can add features based on your needs. Sample Code:

Http://download.csdn.net/detail/qinjuning/4908609 (source code download)





 

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.