The first step is to initialize and obtain an instance of the image picker using the image picker builder. This builder allows you to configure various optional parameters based on the specific needs of your project.
- fragmentActivity: These parameters are used when the image picker is initialized inside the activity, by default is
null
. - fragment: These parameters are used when the image picker is initialized inside the activity, by default is
null
. - coroutineScope: it is a mandatory parameter in the image picker builder. It specifies the coroutine scope in which the image picker operations will run. Typically, you would use the
lifecycleScope
from the activity or fragment, ensuring that the image pickerβs lifecycle is tied to the activity or fragment’s lifecycle, which helps manage tasks such as image selection asynchronously without risking memory leaks or lifecycle issues. - scaleBitmapModelForSingleImage, scaleBitmapModelForMultipleImages, scaleBitmapModelForCameraImage: The parameters
scaleBitmapModelForSingleImage
,scaleBitmapModelForMultipleImages
, andscaleBitmapModelForCameraImage
are used to specify the scaling behavior for different types of images selected through the image picker. Each of these parameters accepts aScaleBitmapModel
object, where you can define the desired width and height for the images. scaleBitmapModelForSingleImage
is applied when the user selects a single image.scaleBitmapModelForMultipleImages
is applied when multiple images are selected.scaleBitmapModelForCameraImage
is applied when an image is taken directly from the camera.
These parameters allow you to control the image size and optimize the images for specific use cases in your application. By default, these values are set to null
, meaning no scaling is applied unless specified.
- enabledBase64ValueForSingleImage, enabledBase64ValueForMultipleImages, enabledBase64ValueForCameraImage: The parameters
enabledBase64ValueForSingleImage
,enabledBase64ValueForMultipleImages
, andenabledBase64ValueForCameraImage
control whether the selected images are encoded into Base64 format. Base64 encoding is useful for transferring or storing image data as strings. enabledBase64ValueForSingleImage
enables Base64 encoding for a single image.enabledBase64ValueForMultipleImages
enables Base64 encoding for multiple images.enabledBase64ValueForCameraImage
enables Base64 encoding for images taken directly from the camera.
By default, these values are set to false
, meaning Base64 encoding is disabled. Setting them to true
allows the images to be returned as Base64 strings, which can be useful for uploading or saving images in a text format.
- imagePickerInterface: It is a callback interface that allows you to handle the result of the image picker operation
class MainActivity : AppCompatActivity(), ImagePickerInterface {
//...
private var imagePicker: ImagePicker? = null//...
fun initImagePicker() {
//Builder
//Note: fragmentActivity or fragment are mandatory one of them
imagePicker = ImagePicker(
fragmentActivity = this, //activity instance - private
fragment = this, // fragment instance - private
coroutineScope = lifecycleScope, // mandatory - coroutine scope from activity or fragment - private
scaleBitmapModelForSingleImage = ScaleBitmapModel(
height = 100,
width = 100
), // optional, change the scale for image, by default is null
scaleBitmapModelForMultipleImages = ScaleBitmapModel(
height = 100,
width = 100
), // optional, change the scale for image, by default is null
scaleBitmapModelForCameraImage = ScaleBitmapModel(
height = 100,
width = 100
), // optional, change the scale for image, by default is null
enabledBase64ValueForSingleImage = true, // optional, by default is false - private
enabledBase64ValueForMultipleImages = true, // optional, by default is false - private
enabledBase64ValueForCameraImage = true, // optional, by default is false - private
imagePickerInterface = this // call back interface
)
//...other image picker initialization method(s)
}
//...
}
These methods are used to initialize different types of image and video pickers in the app, each corresponding to a specific action for selecting or capturing media. Hereβs a breakdown of each:
initPickSingleImageFromGalleryResultLauncher()
This method initializes a launcher for selecting a single image from the deviceβs gallery. Once the user selects an image, the result can be handled using theimagePickerInterface
.initPickMultipleImagesFromGalleryResultLauncher()
This method initializes a launcher for selecting multiple images from the gallery. This is useful when the app needs to allow users to pick more than one image at once. The selected images are then processed by the result callback.initTakePhotoWithCameraResultLauncher()
This method initializes a launcher for capturing a photo using the device’s camera. After the user takes a photo, the result is returned through theimagePickerInterface
, allowing you to handle the newly captured image.initPickSingleVideoFromGalleryResultLauncher()
This method initializes a launcher for selecting a single video from the gallery. After selecting a video, the result is handled by theimagePickerInterface
, enabling you to process or display the video.
Each of these methods is tied to a specific use case, and they all integrate with the ImagePicker
class to facilitate different media selection and capture tasks within the app.
imagePicker?.initPickSingleImageFromGalleryResultLauncher()imagePicker?.initPickMultipleImagesFromGalleryResultLauncher()
imagePicker?.initTakePhotoWithCameraResultLauncher()
imagePicker?.initPickSingleVideoFromGalleryResultLauncher()
Each overridden method corresponds to a specific media type (image or video) selected either from the gallery or captured via the camera.
onGallerySingleImage(bitmap: Bitmap?, uri: Uri?)
This method is triggered when a single image is selected from the gallery. It receives the image as aBitmap
and its URI. You can handle the image (e.g., display it or process it) inside this method.onCameraImage(bitmap: Bitmap?)
This method is called when a photo is taken using the device’s camera. It receives the photo as aBitmap
, which can then be processed as needed.onMultipleGalleryImages(bitmapList: MutableList
?, uriList: MutableList ?)
This method is triggered when multiple images are selected from the gallery. It provides a list ofBitmaps
and a list ofUris
for the selected images. You can use these lists to handle or display the images.onGallerySingleImageWithBase64Value(bitmap: Bitmap?, uri: Uri?, base64AsString: String?)
This method is called when a single image is selected from the gallery, with the additional Base64 encoded string of the image. The Base64 string can be useful if you need to upload or store the image as a string rather than as a file.onCameraImageWithBase64Value(bitmap: Bitmap?, base64AsString: String?)
This method is similar toonCameraImage()
, but it also includes the Base64 string of the captured image. You can use the string for tasks like uploading or saving the image in a database.onMultipleGalleryImagesWithBase64Value(bitmapList: MutableList
?, uriList: MutableList ?, base64AsStringList: MutableList ?)
This method handles the selection of multiple images from the gallery, with the Base64 encoded strings of the images. The method receives lists ofBitmaps
,Uris
, and Base64 strings, allowing you to manage both the visual and string representations of the images.onGallerySingleVideo(uri: Uri?)
This method is triggered when a single video is selected from the gallery. It receives the URI of the selected video, which can be used to play or process the video.
class MainActivity : AppCompatActivity(), ImagePickerInterface {
//...
override fun onGallerySingleImage(bitmap: Bitmap?, uri: Uri?) {
super.onGalleryImage(bitmap, uri)
//...your code here
}override fun onCameraImage(bitmap: Bitmap?) {
super.onCameraImage(bitmap)
//...your code here
}
override fun onMultipleGalleryImages(
bitmapList: MutableList?,
uriList: MutableList?
) {
super.onMultipleGalleryImages(bitmapList, uriList)
//...your code here
}
override fun onGallerySingleImageWithBase64Value(
bitmap: Bitmap?,
uri: Uri?,
base64AsString: String?
) {
super.onGalleryImage(bitmap, uri, base64AsString)
//...your code here
}
override fun onCameraImageWithBase64Value(bitmap: Bitmap?, base64AsString: String?) {
super.onCameraImage(bitmap, base64AsString)
//...your code here
}
override fun onMultipleGalleryImagesWithBase64Value(
bitmapList: MutableList?,
uriList: MutableList?,
base64AsStringList: MutableList?
) {
super.onMultipleGalleryImages(bitmapList, uriList, base64AsStringList)
//...your code here
}
override fun onGallerySingleVideo(uri: Uri?) {
super.onGallerySingleVideo(uri)
//...your code here
}
}