The FileSelector library (available as an AAR) allows for sophisticated file picking with constraints on type, count, and size.
Single Selection (e.g., Images)
Configure FileSelectOptions to define constraints like fileType, singleFileMaxSize, and allFilesMaxSize. Use .filter() to implement custom logic (e.g., excluding GIFs).
Multi-Selection (Multiple Types)
To select different types of files simultaneously (e.g., 1-2 images AND 2-3 audio files), use .setMultiSelect() and .applyOptions(...) with multiple FileSelectOptions objects.
Important Strategy: When using multi-selection, it is recommended to use .setOverLimitStrategy(OVER_LIMIT_EXCEPT_OVERFLOW). This ensures that if one type fails to meet its minimum count requirement, it is simply omitted from the results rather than causing the entire selection process to fail.
val optionsImage = FileSelectOptions().apply {
fileType = FileType.IMAGE
minCount = 1
maxCount = 2
singleFileMaxSize = 5242880
allFilesMaxSize = 10485760
fileCondition = object : FileSelectCondition {
override fun accept(fileType: IFileType, uri: Uri?): Boolean {
return (fileType == FileType.IMAGE && uri != null && !FileUtils.isGif(uri))
}
}
}
FileSelector.with(this)
.setMultiSelect()
.setRequestCode(REQUEST_CHOOSE_FILE)
.setOverLimitStrategy(OVER_LIMIT_EXCEPT_OVERFLOW)
.applyOptions(optionsImage, optionsAudio, optionsTxt)
.callback(object : FileSelectCallBack {
override fun onSuccess(results: List<FileSelectResult>?) { /* handle success */ }
override fun onError(e: Throwable?) { /* handle error */ }
})
.choose()