-
Notifications
You must be signed in to change notification settings - Fork 142
Implementation
-
Start by creating an instance of DialogProperties.
DialogProperties properties = new DialogProperties();
-
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.offset = new File(DialogConfigs.DEFAULT_DIR); properties.extensions = null;
-
Next create an instance of FilePickerDialog, and pass
Contextand DialogProperties references as parameters.FilePickerDialog dialog = new FilePickerDialog(MainActivity.this,properties); dialog.setTitle("Select a File");
-
Next, Attach
DialogSelectionListenerto FilePickerDialog as below,dialog.setDialogSelectionListener(new DialogSelectionListener() { @Override public void onSelectedFilePaths(String[] files) { //files is an array of the paths of files selected by the Application User. } });
An array of paths is returned whenever user press the
selectbutton. -
Use
dialog.show()method to show dialog.That's It. You are good to move further.
- In case no file/directory is selected
onSelectedFilePathsmethod returns an empty array.
Marshmallow and further requests for the permission on runtime. You should override onRequestPermissionsResult of Activity/AppCompatActivity class and show the dialog only if permissions have been granted.
//Add this method to show Dialog, only when the required permissions have been granted to the app.
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String permissions[],@NonNull int[] grantResults) {
switch (requestCode) {
case FilePickerDialog.EXTERNAL_READ_PERMISSION_GRANT: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(dialog != null) {
//Show dialog if the read permission has been granted.
dialog.show();
}
}
else {
//Permission has not been granted. Notify the user.
Toast.makeText(MainActivity.this,"Permission is Required for getting list of files",Toast.LENGTH_SHORT).show();
}
}
}
}-
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:offset_dir="/mnt/sdcard/android" app:error_dir="/mnt" app:root_dir="/sdcard" app:selection_mode="multi_mode" app:selection_type="dir_select" app:extensions="txt:pdf:"/>
-
Implement Preference.OnPreferenceChangeListener to class requiring selected values and
OverrideonPreferenceChange(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; }
-
Typecast
Object ointoStringObject and usesplit(String)function inStringclass 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.
-
defaultValue,error_dir,root_dirandoffset_dirmust have valid directory/file paths. -
defaultValuepaths should end with ':'. -
defaultValuecan have multiple paths, there should be a ':' between two paths. -
extensionsmust not have '.'. -
extensionsshould end with ':' , also have ':' between two extensions. eg. /sdcard:/mnt:
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:'.