android-file-chooser

Additional

Language
Java
Version
N/A
Created
Jun 7, 2015
Updated
Mar 12, 2019
Owner
hedzr
Contributors
hedzr
bostrot
banlyst
Guiorgy
4
Activity
Badge
Generate
Download
Source code
APK file

Promotion

android-file-chooser

android-file-library is a lightweight file/folder chooser.

Overview

Snapshots

More images (beyond v1.1.16) have been found at Gallery

Demo Application

A demo-app can be installed from Play Store.

Changes

x1.2.0 branch

  • In progress
  • Plan: uses AndroidX
  • Done:
    • Keyboard supports: process SPACE and ENTER up event;
    • file list no focus when dialog first showing;
    • better storage media detect algorithm for Android M+;
    • no WRITE_EXTERNAL_STORAGE requests if not enableOptions(true);
    • after requested permissions, try showing dialog again instead of return directly;

v1.1.x patches on master

v1.1.16

  • #48: add displayPath(boolean), thank you @Guiorgy, and your android-smbfile-chooser.

  • new style demo app by @Guiorgy.

  • bugs fixed

  • NOTE: displayPath is true by default now.

  • bumped targer sdk to 1.8 (please include the following into your build.gradle)

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Archived History:

  • no WRITE_EXTERNAL_STORAGE requests if not enableOptions(true);
  • after requested permissions, try showing dialog again instead of return directly;
  • #42: onBackPressedListener not fired. Now, use withCancelListener to handle back key. see also below
  • #45: add titleFollowsDir(boolean) to allow title following the change of current directory.
  • create new folder on the fly, and the optional multiple select mode for developer, thx @Guiorgy.
  • Up (..) on the primary storage root will be replaced with .. SDCard, it allows to jump to external storage such as a SDCard and going back available too.
  • DPad supports, arrow keys supports (#30)

Usages

Configuration

build.gradle

android-file-chooser was released at jcenter, declare deps with:

implementation 'com.obsez.android.lib.filechooser:filechooser:$android_file_chooser_version'

for the newest version(s), looking up the badges above.

taste the fresh

there is a way to taste the master branch with jitpack.io:

  1. add the jitpack repository url to your root build.gradle:
allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
  1. import android-file-chooser
implementation 'com.github.hedzr:android-file-chooser:master-SNAPSHOT'
// implementation 'com.github.hedzr:android-file-chooser:v1.1.14'

Tips for using JitPack.io

To disable gradle local cache in your project, add stretegy into your top build.grable:

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
    resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'
}

ref: https://github.com/spring-gradle-plugins/dependency-management-plugin/issues/74#issuecomment-182484694

Sometimes it's right, sometimes ... no more warrants.

Codes

Tips

  1. I am hands down AlertDialog.
  2. Any codes about ChooserDialog, such as the following demo codes, should be only put into UI thread.

FileChooser android library give a simple file/folder chooser in single call (Fluent):

Choose a Folder

    new ChooserDialog(MainActivity.this)
            .withFilter(true, false)
         .withStartFile(startingDir)
         // to handle the result(s)
            .withChosenListener(new ChooserDialog.Result() {
                @Override
                public void onChoosePath(String path, File pathFile) {
                    Toast.makeText(MainActivity.this, "FOLDER: " + path, Toast.LENGTH_SHORT).show();
                }
            })
            .build()
            .show();

Choose a File

    new ChooserDialog(MainActivity.this)
            .withStartFile(path)
            .withChosenListener(new ChooserDialog.Result() {
                @Override
                public void onChoosePath(String path, File pathFile) {
                    Toast.makeText(MainActivity.this, "FILE: " + path, Toast.LENGTH_SHORT).show();
                }
            })
         // to handle the back key pressed or clicked outside the dialog:
         .withOnCancelListener(new DialogInterface.OnCancelListener() {
       public void onCancel(DialogInterface dialog) {
           Log.d("CANCEL", "CANCEL");
           dialog.cancel(); // MUST have
       }
   })
            .build()
            .show();

Wild-match

    new ChooserDialog(MainActivity.this)
            .withFilter(false, false, "jpg", "jpeg", "png")
            .withStartFile(path)
            .withResources(R.string.title_choose_file, R.string.title_choose, R.string.dialog_cancel)
            .withChosenListener(new ChooserDialog.Result() {
                @Override
                public void onChoosePath(String path, File pathFile) {
                    Toast.makeText(MainActivity.this, "FILE: " + path, Toast.LENGTH_SHORT).show();
                }
            })
            .build()
            .show();

Regex filter

    new ChooserDialog(MainActivity.this)
            .withFilterRegex(false, false, ".*\\.(jpe?g|png)")
            .withStartFile(path)
            .withResources(R.string.title_choose_file, R.string.title_choose, R.string.dialog_cancel)
            .withChosenListener(new ChooserDialog.Result() {
                @Override
                public void onChoosePath(String path, File pathFile) {
                    Toast.makeText(NewMainActivity.this, "FILE: " + path, Toast.LENGTH_SHORT).show();
                }
            })
            .build()
            .show();

Date Format String

Since 1.1.3, new builder options withDateFormat(String) added.

    new ChooserDialog(MainActivity.this)
            .withFilter(true, false)
            .withStartFile(startingDir)
            .withDateFormat("HH:mm")    // see also SimpleDateFormat format specifiers
            .withChosenListener(new ChooserDialog.Result() {
                @Override
                public void onChoosePath(String path, File pathFile) {
                    Toast.makeText(MainActivity.this, "FOLDER: " + path, Toast.LENGTH_SHORT).show();
                }
            })
            .build()
            .show();

Modify Icon or View Layout of AlertDialog:

Since 1.1.6, 2 new options are available:

    new ChooserDialog(MainActivity.this)
            .withFilter(true, false)
            .withStartFile(startingDir)
            .withIcon(R.drawable.ic_file_chooser)
            .withLayoutView(R.layout.alert_file_chooser)
            .withChosenListener(new ChooserDialog.Result() {
                @Override
                public void onChoosePath(String path, File pathFile) {
                    Toast.makeText(MainActivity.this, "FOLDER: " + path, Toast.LENGTH_SHORT).show();
                }
            })
            .build()
            .show();

Customizable NegativeButton

1.1.7 or Higher, try withNegativeButton() and withNegativeButtonListener() instead of withOnBackPressedListener().