Skip to content

ShiftHackZ/ImagePicker

Repository files navigation

ImagePicker Android Library

Android library that can be used as quick solution to ImagePicker feature implementation.

Features

  • Permission handle requests
  • Camera photo picker
  • Gallery single photo picker
  • Gallery multiple photo picker
  • Custom gallery picker, supports multiple selection (for old non-AOSP Android ROMs that does not support multiple selection intent)

Translations

Language Translation Coverage
English 100%
Hebrew 100%
Ukrainian 100%

Implementation

  1. In project-level gradle add new maven repository:
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
  1. In app-level gradle add new implementation:
dependencies {
    implementation 'com.github.ShiftHackZ:ImagePicker:v2.0'
}
  1. Create file provider_path.xml in res/xml folder:
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="media" path="." />
    <external-path name="external_files" path="."/>
</paths>
  1. In your AndroidManifest.xml add the file provider inside the <application tag:
<provider 
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider" 
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data 
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_path" />
</provider>
  1. Add required permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="29" />
  1. In order to receive images, implement ImagePickerCallback in your Fragment/Activity or as object:
class MainActivity : AppCompatActivity(), ImagePickerCallback {

    override fun onImagePickerResult(result: PickedResult) {
        when (result) {
            PickedResult.Empty -> {
                // No file was selected, noting to do
            }
            is PickedResult.Error -> {
                val throwable = result.throwable
                // Some error happened, handle this throwable
            }
            is PickedResult.Multiple -> {
                val pickedImages = result.images
                val files = pickedImages.map { it.file }
                // Selected multiple images, do whatever you want with files
            }
            is PickedResult.Single -> {
                val pickedImage = result.image
                val file = pickedImage.file
                // Selected one image, do whatever you want with file
            }
        }
    }
}
  1. Create an instance of ImagePicker using ImagePicker.Builder(), which require 2 mandatory params: current Activity and ImagePickerCallback:
val imagePicker = ImagePicker.Builder(this.packageName + ".provider", this)
    .useGallery(true)                           // Use gallery picker if true
    .useCamera(true)                            // Use camera picker if true
    .autoRotate(true)                           // Returns 0 degress rotated images, instead of exif-rotated images if true
    .multipleSelection(true)                    // Allow multiple selection in gallery picker
    .minimumSelectionCount(2)                   // Defines min count of GallerySelector.CUSTOM multiple selection gallery picker
    .maximumSelectionCount(3)                   // Defines max count of GallerySelector.CUSTOM multiple selection gallery picker
    .galleryPicker(GalleryPicker.CUSTOM)        // Available values: GalleryPicker.NATIVE, GalleryPicker.CUSTOM
    .build()
  1. Finally, launch your ImagePicker:
imagePicker.launch(context)

Screenshots

Credits