ImagePicker

Additional

Language
Kotlin
Version
1.4.3 (Jun 30, 2020)
Created
Aug 4, 2016
Updated
Nov 13, 2021
Owner
Nguyen Hoang Lam (nguyenhoanglam)
Contributors
Rainer-Lang
Behrouz Khezry (bkhezry)
Nguyen Hoang Lam (nguyenhoanglam)
Serhii Bosovskyi (boss1088)
Peng Peng (brainsucker)
Beta Kuang (beta)
Alexandr (alexandr2levin)
Kishan Viramgama (KishanViramgama)
Chai (nubpro)
Andreas Dybdahl (andybdahl)
10
Activity
Badge
Generate
Download
Source code
APK file

Show card

ImagePicker

An android library which help selecting images from the device with customizable UI.

Preview

Changelog

  • Reimplement underlying logic to adapt to Android Q and later.
  • Replace startActivityForResult with ActivityResultLauncher mechanism.
  • Add and rename some configuration attributes: isLightStatusBar, imageGridCount ...
  • Remove id and path attribute from Image class.
  • Make some small UI changes.

Installation

In settings.gradle file, add JitPack maven like below:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the following dependency in app build.gradle:

dependencies {
    implementation 'com.github.nguyenhoanglam:ImagePicker:1.5.1'
}

You have to migrate your project to support AndroidX by add following lines on gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

For any Java projects, please follow this guide to add Kotlin to existing app.

Usage

Define an ActivityResultLauncher class variable in Activity or Fragment.

private val launcher = registerImagePicker { images ->
    // Selected images are ready to use
    if(images.isNotEmpty()){
        val sampleImage = images[0]
        Glide.with(this@MainActivity)
             .load(sampleImage.uri)
             .into(imageView)
    }
}

Then, launch image picker when needed.

  • With default configuration:
launcher.launch()
  • With customize configuration:
val config = ImagePickerConfig(
    statusBarColor = "#000000",
    isLightStatusBar = false,
    isFolderMode = true,
    isMultipleMode = true,
    maxSize = 10,
    rootDirectory = Config.ROOT_DIR_DOWNLOAD,
    subDirectory = "Photos",
    folderGridCount = GridCount(2, 4),
    imageGridCount = GridCount(3, 5),
    // See more at configuration attributes table below
)

launcher.launch(config)

Configuration attributes

Name Description Default
statusBarColor Status bar color (require API >= 21) #000000
isLightStatusBar Set status bar to light/dark mode to change it's content to dark/light (require API >= 21) false
toolbarColor Toolbar color #212121
toolbarTextColor Toolbar text color #FFFFFF
toolbarIconColor Toolbar icon color #FFFFFF
backgroundColor Background color #424242
progressIndicatorColor Loading indicator color #009688
selectedIndicatorColor Selected image's indicator color #1976D2
isCameraOnly Open camera, then capture and return an image false
isMultipleMode Allow to select multiple images true
isFolderMode Show images by folders false
folderGridCount Set folder colums for portrait and landscape orientation GridCount(2, 4)
imageGridCount Set image colums for portrait and landscape orientation GridCount(3, 5)
doneTitle Done button title DONE
folderTitle Toolbar title for folder mode (require FolderMode = true) Albums
imageTitle Toolbar title for image mode (require FolderMode = false) Photos
isShowCamera Show camera button true
isShowNumberIndicator Show selected image's indicator as number false
isAlwaysShowDoneButton Show done button even though no images've been selected yet false
rootDirectory Public root directory of captured image, should be one of: RootDirectory.DCIM, RootDirectory.PICTURES, RootDirectory.DOWNLOADS. RootDirectory.DCIM
subDirectory Root directory's sub folder of captured image Application name
maxSize Max images can be selected Int.MAX_VALUE
limitMessage Message to be displayed when total selected images exceeds max size ...
selectedImages List of images that will be shown as selected in ImagePicker Empty list

License

Copyright (c) 2021 Nguyen Hoang Lam