A Simple Android Library to capture and pick image from gallery.
Added support for multiple image pick. This library uses @esafirm android-image-picker library for below API level 30 and new photo picker for newer versions.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.abdullahalshafi:BasicImagePicker:1.1.4'
}
ImageUtilHelper.create(this, cameraLauncher) {
isCamera(true)
saveIntoGallery(true)
galleryDirectoryName("MyDirectory")
start()
}
ImageUtilHelper.create(this, galleryLauncher) {
isGallery(true)
start()
}
ImageUtilHelper.create(this, galleryVideoLauncher) {
isGallery(true)
isOnlyVideo(true)
setVideoSizeLimitInMB(10)
start()
}
ImageUtilHelper.create(this, multiImageLauncher) {
multi()
maxImage(5)
start()
}
private val cameraLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val image: BasicImageData =
it.data!!.getSerializableExtra(BasicImageData::class.java.simpleName) as BasicImageData
//do stuffs with the image object
} else if (it.resultCode == Activity.RESULT_CANCELED) {
//handle your own situation
}
}
private val galleryLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val image: BasicImageData =
it.data!!.getSerializableExtra(BasicImageData::class.java.simpleName) as BasicImageData
//do stuffs with the image object
} else if (it.resultCode == Activity.RESULT_CANCELED) {
//handle your own situation
}
}
private val galleryVideoLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val basicImageData: BasicImageData =
it.data!!.getSerializableExtra(BasicImageData::class.java.simpleName) as BasicImageData
Log.d("VIDEO_DATA", "name: ${basicImageData.name} path: ${basicImageData.path}")
} else if (it.resultCode == Activity.RESULT_CANCELED) {
//handle your own situation
}
}
private val multiImageLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val images: List<BasicImageData> =
it.data!!.getSerializableExtra(BasicImageData::class.java.simpleName) as List<BasicImageData>
Log.d("MULTI_IMAGE_DATA", "images: $images")
//do stuffs with the image object
Glide.with(this)
.load(images[0].path)
.into(findViewById(R.id.image_view))
} else if (it.resultCode == Activity.RESULT_CANCELED) {
//handle your own situation
}
}