Lightweight Android File Chooser
Lightweight Android File Chooser
Add jcenter repositories to your project gradle if not present, usually its automatically added by android studio.
In your application build.gradle dependencies add this line, for grandle <3.0 :
compile 'kbrs.com.kfilechooser:kfilechooser:1.0'
for grandle >=3.0 :
implementation 'kbrs.com.kfilechooser:kfilechooser:1.0'
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
KFileChooser.getInstance()
.setRootDir(Environment.getExternalStorageDirectory())
.setOnFileChooserResultListener(new KFileChooser.OnFileChooserResultListener() {
@Override
public void onResult(File fileResult) {
if(fileResult!=null){
//do your action here
}
}
@Override
public void onCancel(File lastChoosed) {
}
})
.show(getSupportFragmentManager());
In your onResult method dont forget to check if the result file is null or not. Null File instance return if nothing file is choosed.
Full usage of this library:
KFileChooser.getInstance()
.setTitle("My File Chooser")
.setRootDir(Environment.getExternalStorageDirectory())
.setSelect(KFileFilter.MODE_SELECT_FILE)
/* KFileFilter.MODE_SELECT_FILE || KFileFilter.MODE_SELECT_FOLDER || KFileFilter.MODE_SELECT_FOLDER_ONLY */
.setExtension("pdf")
/* .setExpression( yourRegularExpression ) */
.setOnFileChooserResultListener(new KFileChooser.OnFileChooserResultListener() {
@Override
public void onResult(File fileResult) {
if(fileResult!=null){
//do your action here
}
}
@Override
public void onCancel(File lastChoosed) {
}
})
.show(getSupportFragmentManager());
This library using DialogFragment and this project minimum target in android 15 or later