Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Implementation

Angad Singh edited this page Aug 13, 2016 · 9 revisions

File Picker Dialog

  1. Start by creating an instance of DialogProperties.

        DialogProperties properties=new DialogProperties();
  2. Assign values to each Dialog Property using DialogConfigs class.

        properties.selection_mode=DialogConfigs.SINGLE_MODE;
        properties.selection_type=DialogConfigs.FILE_SELECT;
        properties.root=new File(DialogConfigs.DEFAULT_DIR);
        properties.error_dir=new File(DialogConfigs.DEFAULT_DIR);
        properties.extensions=null;
  3. Next create an instance of FilePickerDialog, and pass Context and DialogProperties references as parameters.

        FilePickerDialog dialog = new FilePickerDialog(MainActivity.this,properties);
  4. Next, Attach DialogSelectionListener to FilePickerDialog as below,

        dialog.setDialogSelectionListener(new DialogSelectionListener() {
            @Override
            public void onSelectedFilePaths(String[] files) {
                //files is the array of the paths of files selected by the Application User.
            }
        });

    An array of paths is returned whenever user press the select button.

  5. Use dialog.show() method to show dialog.

    That's It. You are good to move further.

###Note:

  • In case of no selection onSelectedFilePaths method returns an empty array.

##File Picker Preference

  1. Start by declaring FilePickerPreference in your settings xml file as:

       <com.github.angads25.filepicker.view.FilePickerPreference
           xmlns:app="http://schemas.android.com/apk/res-auto"
           android:key="your_preference_key"
           android:title="Pick a Directory"
           android:summary="Just a Summary"
           android:defaultValue="/sdcard:/mnt"
           app:error_dir="/mnt"
           app:root_dir="/sdcard"
           app:selection_mode="multi_mode"
           app:selection_type="dir_select"
           app:extensions="txt:pdf:"/>
  2. Implement Preference.OnPreferenceChangeListener to class requiring selected values and Override onPreferenceChange(Preference, Object) method. Check for preference key using Preference reference.

        @Override
        public boolean onPreferenceChange(Preference preference, Object o) 
        {   if(preference.getKey().equals("your_preference_key"))
            {   ...
            }
            return false;
        }
  3. Typecast Object o into String Object and use split(String) function in String class to get array of selected files.

        @Override
        public boolean onPreferenceChange(Preference preference, Object o) 
        {   if(preference.getKey().equals("your_preference_key"))
            {   String value=(String)o;
                String arr[]=value.split(":");
                ...
                ...
            }
            return false;
        }

    That's It. You are good to move further.

###Important:

  • defaultValue, error_dir, root_dir must have valid directory/file paths.
  • defaultValue paths should end with ':'.
  • defaultValue can have multiple paths, there should be a ':' between two paths.
  • extensions must not have '.'.
  • extensions should end with ':' , also have ':' between two extensions. eg. /sdcard:/mnt:

###Note: FilePickerPreference stores selected directories/files as a String. It delimits multiple files/directories using ':' char.

For eg.

If image1.png and image2.png are the two files selected by the user from '/sdcard', the FilePickerPreference will store it in a form of single String as '/sdcard/image1.png:/sdcard/image2.png:'.

Clone this wiki locally