Skip to content

MostafaNasiri/AndroidFileChooser

Repository files navigation

AndroidFileChooser (This library is no longer maintained)

Android File Chooser is a simple and customizable file/directory chooser fragment which you can use in your apps to let your users select a file or directory based on your needs.

How to Add the Library

This library is availabe in the jcenter repository. Simply add this line of code in your dependencies:

compile 'ir.sohreco.androidfilechooser:android-file-chooser:1.3'

How to Use

If you want the default look for your file/directory chooser you can simply implement FileChooser.ChooserListener in your class and create an instance of FileChooser.Builder and then customize your FileChooser with many methods that are provided:

FileChooser.Builder builder = new FileChooser.Builder(FileChooser.ChooserType.FILE_CHOOSER, this)
    .setMultipleFileSelectionEnabled(true)
    .setFileFormats(new String[] {".jpg", ".png"})
    .setListItemsTextColor(R.color.colorPrimary)
    .setPreviousDirectoryButtonIcon(R.drawable.ic_prev_dir)
    .setDirectoryIcon(R.drawable.ic_directory)
    .setFileIcon(R.drawable.ic_file)
    // And more...
    ;

Notice that the first parameter is the chooser type which you should select from the ChooserType enum and the second parameter is the class that implements FileChooser.ChooserListener

try {
  FileChooser fileChooserFragment = builder.build();
} catch (ExternalStorageNotAvailableException e) {
  e.printStackTrace();
}

You should catch ExternalStorageNotAvailableException when you want to make an instance of the fragment by calling build().

How to Use Multiple File Selection Feature

When multiple file selection is enabled, the path parameter in onSelect method of the ChooserListener is a string containing selected files paths seperated by FileChooser.FILE_NAMES_SEPARATOR.

FileChooser.Builder builder = new FileChooser.Builder(FileChooser.ChooserType.FILE_CHOOSER, new                FileChooser.ChooserListener() {
            @Override
            public void onSelect(String path) {
                String[] selectedFilePaths = path.split(FileChooser.FILE_NAMES_SEPARATOR);
                // Do whatever you want to do with selected files
            }
        })
                .setMultipleFileSelectionEnabled(true)
                .setSelectMultipleFilesButtonText("Select Files");

On Android Version 6 And Above

You should grant READ_EXTERNAL_STORAGE permission:

int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
} else {
    // Your app already has the permission to access files and folders
    // so you can simply open FileChooser here.
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PERMISSION_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted.
        }
    }
}

About

A customizable file/directory chooser for Android.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages