When it comes to selecting files in an Android application, using a file picker library can greatly simplify the process. In this tutorial, we will explore some popular file picker libraries for Kotlin Android like: Android-FilePicker, FilePickerLibrary, and MaterialFilePicker.
1. Android-FilePicker:
Android-FilePicker is a simple and easy-to-use file picker library for Android. It allows you to select files from the device's internal and external storage.
Features:
- Supports selecting files from internal and external storage
- Allows filtering files by type
- Provides a simple and customizable UI
- Supports both single and multiple file selection
Installation: To install Android-FilePicker, add the following dependency to your app-level build.gradle file:
dependencies {
implementation 'com.github.angads25:filepicker:1.1.1'
}
Code Examples: To use Android-FilePicker, first, create a new instance of the FilePicker class:
val filePicker = FilePicker()
Next, set the file types you want to allow the user to select:
filePicker.setFileTypes("image/*", "application/pdf")
Then, set the maximum number of files the user can select:
filePicker.setMaxCount(5)
Finally, launch the file picker activity:
filePicker.launch(this, REQUEST_CODE_FILE_PICKER)
In the onActivityResult method, handle the selected files:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_FILE_PICKER && resultCode == Activity.RESULT_OK) {
val files = data?.getStringArrayListExtra(FilePickerConstants.KEY_SELECTED_MEDIA)
// Do something with the selected files
}
}
Comparison: Android-FilePicker is a simple and easy-to-use file picker library. It provides a customizable UI and supports both single and multiple file selection. However, it does not provide as many features as the other libraries.
Reference Link: https://github.com/angads25/android-filepicker
2. FilePickerLibrary:
FilePickerLibrary is a powerful and customizable file picker library for Android. It allows you to select files from the device's internal and external storage, as well as from cloud storage services like Google Drive and Dropbox.
Features:
- Supports selecting files from internal and external storage, as well as cloud storage services
- Allows filtering files by type
- Provides a customizable UI
- Supports both single and multiple file selection
- Allows customizing the file picker activity
Installation: To install FilePickerLibrary, add the following dependency to your app-level build.gradle file:
dependencies {
implementation 'com.nononsenseapps:filepicker:5.0.2'
}
Code Examples: To use FilePickerLibrary, first, create a new instance of the FilePickerActivity class:
val filePicker = FilePickerActivity()
Next, set the file types you want to allow the user to select:
filePicker.setFileTypes("image/*", "application/pdf")
Then, set the maximum number of files the user can select:
filePicker.setMaxCount(5)
Finally, launch the file picker activity:
filePicker.launch(this, REQUEST_CODE_FILE_PICKER)
In the onActivityResult method, handle the selected files:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_FILE_PICKER && resultCode == Activity.RESULT_OK) {
val files = data?.getSerializableExtra(FilePickerActivity.EXTRA_FILES) as ArrayList<File>
// Do something with the selected files
}
}
Comparison: FilePickerLibrary is a powerful and customizable file picker library. It provides a customizable UI and supports both single and multiple file selection. It also supports selecting files from cloud storage services. However, it may be more complex to use than the other libraries.
Reference Link: https://github.com/spacecowboy/NoNonsense-FilePicker
3. MaterialFilePicker:
MaterialFilePicker is a material design file picker library for Android. It allows you to select files from the device's internal and external storage.
Features:
- Supports selecting files from internal and external storage
- Allows filtering files by type
- Provides a material design UI
- Supports both single and multiple file selection
Installation: To install MaterialFilePicker, add the following dependency to your app-level build.gradle file:
dependencies {
implementation 'com.nbsp:materialfilepicker:1.10.0'
}
Code Examples: To use MaterialFilePicker, first, create a new instance of the MaterialFilePicker class:
val filePicker = MaterialFilePicker()
Next, set the file types you want to allow the user to select:
filePicker.withFilter(Pattern.compile(".*\\.(jpg|jpeg|png|pdf)"))
Then, set the maximum number of files the user can select:
filePicker.withMaxCount(5)
Finally, launch the file picker activity:
filePicker.withActivity(this)
filePicker.withRequestCode(REQUEST_CODE_FILE_PICKER)
filePicker.start()
In the onActivityResult method, handle the selected files:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_FILE_PICKER && resultCode == Activity.RESULT_OK) {
val files = data?.getStringArrayListExtra(MaterialFilePickerActivity.RESULT_FILE_PATH)
// Do something with the selected files
}
}
Comparison: MaterialFilePicker is a material design file picker library. It provides a material design UI and supports both single and multiple file selection. However, it does not support selecting files from cloud storage services.
Reference Link: https://github.com/nbsp-team/MaterialFilePicker
4. MaterialFilePicker
Introduction: MaterialFilePicker is a lightweight and customizable file chooser library for Android that follows Material Design guidelines. It allows users to select files or directories from local storage.
Features:
- Material Design UI.
- Single and multiple file selection modes.
- Ability to select directories.
- Customizable file filters.
- Easy to use and integrate.
Installation:
Add the dependency to your build.gradle
(Module: app) file:
dependencies {
implementation 'com.nbsp:materialfilepicker:4.3.0'
}
Code Example:
val intent = MaterialFilePicker()
.withActivity(this)
.withRequestCode(1)
.withFilter(Pattern.compile(".*\\.txt$")) // Filter to only show .txt files
.withTitle("Pick a file")
.start()
In the onActivityResult
method, handle the result:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
val filePath = data?.getStringExtra(FilePickerActivity.RESULT_FILE_PATH)
// Use the file path as needed
}
}
Comparison: MaterialFilePicker stands out for its adherence to Material Design and ease of use. It's a good choice if you want a simple and good-looking file picker without too many bells and whistles.
Reference Link: MaterialFilePicker on GitHub
5. Android-File-Dialogs
Introduction: Android-File-Dialogs is a library that provides simple file picker dialogs for Android. It supports both single and multiple file selections.
Features:
- Simple and easy-to-use dialogs.
- Single and multiple file selection.
- Customizable UI with themes.
- Filter for file types.
Installation:
Add the dependency to your build.gradle
(Module: app) file:
dependencies {
implementation 'com.simplemobiletools:file-dialog:4.0.2'
}
Code Example:
val fileDialog = FilePickerDialog(this)
fileDialog.setTitle("Select a File")
fileDialog.setPositiveButton(
"OK"
) { dialog, which ->
val selectedFile = fileDialog.getFile()
// Use the selected file as needed
}
fileDialog.setNegativeButton(
"CANCEL",
null
)
fileDialog.show()
Comparison: Compared to MaterialFilePicker, Android-File-Dialogs offers a more straightforward dialog-based approach. It's suitable for developers who prefer a minimalistic and straightforward file picker.
Reference Link: Android-File-Dialogs on GitHub
6. aFileChooser
Introduction: aFileChooser is a file chooser library for Android that provides a simple and customizable file picking experience. It's known for its robustness and flexibility.
Features:
- Both single and multiple file selections.
- Customizable UI.
- Support for creating files and directories.
- Built-in image and audio previews.
Installation:
Add the dependency to your build.gradle
(Module: app) file:
dependencies {
implementation 'com.ipaulpro:afilechooser:3.0.1'
}
Code Example:
val intent = Intent(this, FileChooserActivity::class.java)
intent.putExtra(FileChooserActivity.EXTRA_ALLOW_MULTIPLE, true)
intent.putExtra(FileChooserActivity.EXTRA_INITIAL_PATH, Environment.getExternalStorageDirectory().path)
startActivityForResult(intent, 1)
In the onActivityResult
method, handle the result:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
val result = FileChooserActivity.getResults(data)
// Handle the list of selected files
}
}
Comparison: aFileChooser is more feature-rich compared to the other two libraries. It's a good fit for applications that require more advanced file operations and previews.
Reference Link: aFileChooser on GitHub