大家都知道我们直接调取相册都是只能选择一张照片,但是如何多选呢,今天跟大家分享一个好用的框架
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f3c0120ba22dda2d53d5ec867a173ae8.png#pic_center)
第一步:添加依赖
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.3.7'
第二步:调取配置属性
1.编写工具类
class GlideEngine private constructor() : ImageEngine {
override fun loadImage(context: Context, url: String, imageView: ImageView) {
Glide.with(context).load(url).into(imageView)
}
override fun loadFolderAsBitmapImage(
context: Context, url: String,
imageView: ImageView, placeholderId: Int
) {
Glide.with(context)
.asBitmap()
.override(180, 180)
.centerCrop()
.sizeMultiplier(0.5f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(placeholderId)
.load(url)
.into(object : BitmapImageViewTarget(imageView) {
override fun setResource(resource: Bitmap?) {
val circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.resources, resource)
circularBitmapDrawable.cornerRadius = 8f
imageView.setImageDrawable(circularBitmapDrawable)
}
})
}
override fun loadAsGifImage(
context: Context, url: String,
imageView: ImageView
) {
Glide.with(context)
.asGif()
.load(url)
.into(imageView)
}
override fun loadAsBitmapGridImage(
context: Context, url: String,
imageView: ImageView, placeholderId: Int
) {
Glide.with(context)
.asBitmap()
.override(200, 200)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(placeholderId)
.load(url)
.into(imageView)
}
companion object {
val createGlideEngine: GlideEngine by lazy { GlideEngine() }
}
}
2.调用
PictureSelector.create(this).openGallery(PictureMimeType.ofImage())
.isCamera(true)
.loadImageEngine(GlideEngine.createGlideEngine)
.selectionMode(PictureConfig.SINGLE)
.forResult(PictureConfig.CHOOSE_REQUEST)
第三步:获取回调返回路径
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
PictureConfig.CHOOSE_REQUEST ->
val selectList = PictureSelector.obtainMultipleResult(data)
}
}
}