Android File/folder selector (support multi-select operation), has been encapsulated as Lib library, directly add dependencies.

Source: Internet
Author: User

Not a few words, first glance:


Next we start to write an app test:

1. New Android Project: Fileselectortest

2. Change Mainactivity:

In the inside write four TextView analog button, is the file single-choice, file multi-Select, Folder path single-selection, folder path multi-select, and then rewrite TextView Click event into fsactivity, incoming parameters, different selection mode.

A replication onactivityresult that is used to obtain data that is passed back from the file/folder selector.

 Packagecn.darkranger.fileselectortest;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;Importdr.android.fileselector.FileSelectConstant; Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Mainactivity"; Private Static FinalInteger File_selector_request_code = 2016; PrivateTextView Mfiletv; PrivateTextView Mfilemultitv; PrivateTextView Mfoldertv; PrivateTextView Mfoldermultitv; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Initview ();        InitData ();    Initevent (); }    Private voidInitview () {Mfiletv=(TextView) Findviewbyid (r.id.id_mode_file); MFILEMULTITV=(TextView) Findviewbyid (R.id.id_mode_file_multi); Mfoldertv=(TextView) Findviewbyid (R.id.id_mode_folder); MFOLDERMULTITV=(TextView) Findviewbyid (R.id.id_mode_folder_multi); }    Private voidInitData () {}Private voidinitevent () {//File RadioMfiletv.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (Getapplicationcontext (), fsactivity.class);                Intent.putextra (Fileselectconstant.selector_request_code_key, fileselectconstant.selector_mode_file);            Startactivityforresult (Intent, File_selector_request_code);        }        }); //file Multiple selectionMfilemultitv.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (Getapplicationcontext (), fsactivity.class);                Intent.putextra (Fileselectconstant.selector_request_code_key, fileselectconstant.selector_mode_file); Intent.putextra (Fileselectconstant.selector_is_multiple,true);            Startactivityforresult (Intent, File_selector_request_code);        }        }); //folder RadioMfoldertv.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (Getapplicationcontext (), fsactivity.class);                Intent.putextra (Fileselectconstant.selector_request_code_key, Fileselectconstant.selector_mode_folder);            Startactivityforresult (Intent, File_selector_request_code);        }        }); //folder multiple selectionMfoldermultitv.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (Getapplicationcontext (), fsactivity.class);                Intent.putextra (Fileselectconstant.selector_request_code_key, Fileselectconstant.selector_mode_folder); Intent.putextra (Fileselectconstant.selector_is_multiple,true);            Startactivityforresult (Intent, File_selector_request_code);    }        }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent bundle) {        Super. Onactivityresult (Requestcode, ResultCode, bundle); LOG.I (TAG,"Requestcode:" + Requestcode + "; ResultCode: "+ResultCode); if(ResultCode = =RESULT_OK) {Toast.maketext ( This, "Paths:" +Bundle.getstringarraylistextra (fileselectconstant.selector_bundle_paths), Toast.length_short). Show        (); }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

3. New Fsactivity

(1) Add Dependency package Dr-lib-fileselector:
  

(2) Succession fileselectactivity

(3) OnCreate, Oncreateoptionsmenu and onoptionsitemselected Direct use of Fileselectactivity method, super.xxx ();

(4) The method of carbon fileselectactivity InitParams, which set up some basic information, including the selection of the title of the file, select the folder path title, folder icon, file icon, icon width and height, of course, do not write the words are used default values.

(5) can also be replicated onbackpressed () and ONCLICKOKBTN (), respectively, the following virtual return key operation and the upper right corner of the selection after the completion of the operation. Of course, it's all done with default fileselectactivity.

 Packagecn.darkranger.fileselectortest;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;Importdr.android.fileselector.FileSelectActivity; Public classFsactivityextendsfileselectactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {return Super. Oncreateoptionsmenu (menu); } @Override Public Booleanonoptionsitemselected (MenuItem item) {return Super. onoptionsitemselected (item); } @Override Public voidInitParams () {//These parameters have default values and are not set.//Setselectorfiletitle ("This is file title"); //Setselectorfoldertitle ("This is the folder title"); //Setselectorfileicon (R.drawable.ic_fileselect_file); //Setselectorfoldericon (R.drawable.ic_fileselect_folder); //Setselectoriconheight (150); //setselectoriconwidth ();}} @Override Public voidonbackpressed () {Super. onbackpressed (); } @Override Public voidonclickokbtn () {Super. ONCLICKOKBTN (); }}

4. Add read and write storage permissions in Androidmanifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Cn.darkranger.fileselectortest"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "All" />    <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permissionAndroid:name= "Android.permission.READ_EXTERNAL_STORAGE" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Cn.darkranger.fileselectortest.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <ActivityAndroid:name= "Cn.darkranger.fileselectortest.FsActivity"Android:label= "@string/title_activity_fs" >        </Activity>    </Application></Manifest>

5, complete the configuration, and run.

Project Source: Http://files.cnblogs.com/files/wrcold520/FileSelectorTest.zip (Lib package inside removed the V4 package, dependent on dr-lib-fileselector)

Dependency Package: http://files.cnblogs.com/files/wrcold520/ Dr-lib-fileselector.zip (Lib package inside removed the V4 package, Dr-lib-fileselector dependency package depends on V7 package, such as, V7 's dependency package has V4 package)

  

Android File/folder selector (support multi-select operation), has been encapsulated as Lib library, directly add dependencies.

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.