安卓 好用的第三方文件、文件夹选择器框架
导入依赖
android {
buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
}
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
}
dependencies{
//实现Android 文件、文件夹选择器,支持多选
implementation 'com.github.ns3chz:Picker:1.0.2'
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:12.3'
}
权限检查
//检测权限
XXPermissions.with(this)
.permission(Permission.MANAGE_EXTERNAL_STORAGE)
.request { permissions, _ ->
Toast.makeText(
this@MainActivity,
permissions.toString(),
Toast.LENGTH_SHORT
).show()
}
使用
//路径
binding.buttonPath.setOnClickListener {
FilePicker
.build(this, 1)
//设置初始打开的目录,默认为sdcard
.setOpenFile(File("sdcard/DCIM/"))
//设置选择文件还是文件夹,默认为文件
.setPickFileType(FilePickerUiParams.PickType.FOLDER)
//设置为单选,setMultiPick为设置多选,不可设置多选文件夹
.setSinglePick(object : FilePicker.OnSinglePickListener() {
override fun pick(file: File) {
path.value = file.absolutePath
val edit = sharedPreferences.edit()
edit.putString(getString(R.string.sp_image_path), path.value)
edit.apply()
}
})
//打开选择界面
.open()
}
添加回调
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//会自动去集合中找到对应的组件、请求码做返回
FilePicker.onActivityResult(this, requestCode, resultCode, data)
}