Looking for a user-friendly and customizable library to handle image selection and capture? Look no further! Our ImagePicker library is here to save the day. With this library, you can easily pick an image from your gallery or capture one using your camera. But that's not all! It also offers cropping and compression functionalities based on aspect ratio, resolution, and image size. As a developer, I understand the importance of image upload features in apps. That's why I've dedicated almost 90% of my development efforts to perfecting this functionality. When it comes to profile images, I've got you covered with the uCrop feature for easy cropping. And for those large image files captured by the camera, our library allows you to compress them to a more manageable size, typically reducing them from 5-10 MBs. But wait, there's more! We also understand that sometimes you need to upload images with specific resolution or size requirements. In such cases, our image compress feature comes in handy, ensuring your images meet the necessary criteria. So, if you're tired of struggling with image pick and capture options, give our ImagePicker library a try. It's designed to simplify the process and make your life as a developer easier. Trust me, you won't be disappointed!
📸Key Features: 1. Select Images from Gallery: Easily pick your favorite images from your gallery to use in your app. 2. Access Google Drive Images: Seamlessly retrieve images from your Google Drive and integrate them into your app. 3. Capture Camera Images: Capture stunning photos directly from your device's camera and use them instantly. 4. Crop Images: Customize your images by cropping them to a specific aspect ratio or let your users choose their preferred cropping style. 5. Compress Images: Optimize your app's performance by compressing images based on desired resolution and size. 6. Retrieve Image Result as Uri: Retrieve image results as Uri objects for easy integration and manipulation within your app. (Note: File object retrieval feature has been removed in v2.0 to support scoped storage) 7. Handle Runtime Permissions: Effortlessly manage runtime permissions for camera access, ensuring a smooth user experience. 8. No Storage Permission Required: Enjoy the convenience of picking gallery images or capturing new images without requiring storage permission.
💻Usage
Gradle dependency:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
implementation 'com.github.dhaval2404:imagepicker:2.1'
If you are yet to Migrate on AndroidX, Use support build artifact:
implementation 'com.github.dhaval2404:imagepicker-support:1.7.1'
The ImagePicker configuration is created using the builder pattern.
Kotlin
ImagePicker.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.start()
Java
Java
ImagePicker.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.start()
Handling results
Override onActivityResult
method and handle ImagePicker result.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
//Image Uri will not be null for RESULT_OK
val uri: Uri = data?.data!!
// Use Uri object instead of File to avoid storage permissions
imgProfile.setImageURI(fileUri)
} else if (resultCode == ImagePicker.RESULT_ERROR) {
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
}
}
Inline method (with registerForActivityResult, Only Works with FragmentActivity and AppCompatActivity)
i. Add required dependency for registerForActivityResult API
implementation "androidx.activity:activity-ktx:1.2.3"
implementation "androidx.fragment:fragment-ktx:1.3.3"
ii. Declare this method inside fragment or activity class
private val startForProfileImageResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
val resultCode = result.resultCode
val data = result.data
if (resultCode == Activity.RESULT_OK) {
//Image Uri will not be null for RESULT_OK
val fileUri = data?.data!!
mProfileUri = fileUri
imgProfile.setImageURI(fileUri)
} else if (resultCode == ImagePicker.RESULT_ERROR) {
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
}
}
iii. Create ImagePicker instance and launch intent
ImagePicker.with(this)
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.createIntent { intent ->
startForProfileImageResult.launch(intent)
}
Gradle dependency:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
implementation 'com.github.dhaval2404:imagepicker:2.1'
If you are yet to Migrate on AndroidX, Use support build artifact:
implementation 'com.github.dhaval2404:imagepicker-support:1.7.1'
The ImagePicker configuration is created using the builder pattern.
Kotlin
ImagePicker.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.start()
Java
Java
ImagePicker.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.start()
Handling results
Override onActivityResult
method and handle ImagePicker result.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
//Image Uri will not be null for RESULT_OK
val uri: Uri = data?.data!!
// Use Uri object instead of File to avoid storage permissions
imgProfile.setImageURI(fileUri)
} else if (resultCode == ImagePicker.RESULT_ERROR) {
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
}
}
Inline method (with registerForActivityResult, Only Works with FragmentActivity and AppCompatActivity)
i. Add required dependency for registerForActivityResult API
implementation "androidx.activity:activity-ktx:1.2.3"
implementation "androidx.fragment:fragment-ktx:1.3.3"
ii. Declare this method inside fragment or activity class
private val startForProfileImageResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
val resultCode = result.resultCode
val data = result.data
if (resultCode == Activity.RESULT_OK) {
//Image Uri will not be null for RESULT_OK
val fileUri = data?.data!!
mProfileUri = fileUri
imgProfile.setImageURI(fileUri)
} else if (resultCode == ImagePicker.RESULT_ERROR) {
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
}
}
iii. Create ImagePicker instance and launch intent
ImagePicker.with(this)
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.createIntent { intent ->
startForProfileImageResult.launch(intent)
}
Customization
Pick image using Gallery
ImagePicker.with(this)
.galleryOnly() //User can only select image from Gallery
.start() //Default Request Code is ImagePicker.REQUEST_CODE
Capture image using Camera
ImagePicker.with(this)
.cameraOnly() //User can only capture image using Camera
.start()
Crop image
ImagePicker.with(this)
.crop() //Crop image and let user choose aspect ratio.
.start()
Crop image with fixed Aspect Ratio
ImagePicker.with(this)
.crop(16f, 9f) //Crop image with 16:9 aspect ratio
.start()
Crop square image(e.g for profile)
ImagePicker.with(this)
.cropSquare() //Crop square image, its same as crop(1f, 1f)
.start()
Compress image size(e.g image should be maximum 1 MB)
ImagePicker.with(this)
.compress(1024) //Final image size will be less than 1 MB
.start()
Set Resize image resolution
ImagePicker.with(this)
.maxResultSize(620, 620) //Final image resolution will be less than 620 x 620
.start()
Intercept ImageProvider, Can be used for analytics
ImagePicker.with(this)
.setImageProviderInterceptor { imageProvider -> //Intercept ImageProvider
Log.d("ImagePicker", "Selected ImageProvider: "+imageProvider.name)
}
.start()
Intercept Dialog dismiss event
ImagePicker.with(this)
.setDismissListener {
// Handle dismiss event
Log.d("ImagePicker", "onDismiss");
}
.start()
Specify Directory to store captured, cropped or compressed images. Do not use external public storage directory (i.e. Environment.getExternalStorageDirectory())
ImagePicker.with(this)
/// Provide directory path to save images, Added example saveDir method. You can choose directory as per your need.
// Path: /storage/sdcard0/Android/data/package/files
.saveDir(getExternalFilesDir(null)!!)
// Path: /storage/sdcard0/Android/data/package/files/DCIM
.saveDir(getExternalFilesDir(Environment.DIRECTORY_DCIM)!!)
// Path: /storage/sdcard0/Android/data/package/files/Download
.saveDir(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!)
// Path: /storage/sdcard0/Android/data/package/files/Pictures
.saveDir(getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!)
// Path: /storage/sdcard0/Android/data/package/files/Pictures/ImagePicker
.saveDir(File(getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!, "ImagePicker"))
// Path: /storage/sdcard0/Android/data/package/files/ImagePicker
.saveDir(getExternalFilesDir("ImagePicker")!!)
// Path: /storage/sdcard0/Android/data/package/cache/ImagePicker
.saveDir(File(getExternalCacheDir(), "ImagePicker"))
// Path: /data/data/package/cache/ImagePicker
.saveDir(File(getCacheDir(), "ImagePicker"))
// Path: /data/data/package/files/ImagePicker
.saveDir(File(getFilesDir(), "ImagePicker"))
// Below saveDir path will not work, So do not use it
// Path: /storage/sdcard0/DCIM
// .saveDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM))
// Path: /storage/sdcard0/Pictures
// .saveDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))
// Path: /storage/sdcard0/ImagePicker
// .saveDir(File(Environment.getExternalStorageDirectory(), "ImagePicker"))
.start()
Limit MIME types while choosing a gallery image
ImagePicker.with(this)
.galleryMimeTypes( //Exclude gif images
mimeTypes = arrayOf(
"image/png",
"image/jpg",
"image/jpeg"
)
)
.start()
You can also specify the request code with ImagePicker
ImagePicker.with(this)
.maxResultSize(620, 620)
.start(101) //Here 101 is request code, you may use this in onActivityResult
Add Following parameters in your colors.xml file, If you want to customize uCrop Activity.
<resources>
<!-- Here you can add color of your choice -->
<color name="ucrop_color_toolbar">@color/teal_500</color>
<color name="ucrop_color_statusbar">@color/teal_700</color>
<color name="ucrop_color_widget_active">@color/teal_500</color>
</resources>
Pick image using Gallery
ImagePicker.with(this)
.galleryOnly() //User can only select image from Gallery
.start() //Default Request Code is ImagePicker.REQUEST_CODE
Capture image using Camera
ImagePicker.with(this)
.cameraOnly() //User can only capture image using Camera
.start()
Crop image
ImagePicker.with(this)
.crop() //Crop image and let user choose aspect ratio.
.start()
Crop image with fixed Aspect Ratio
ImagePicker.with(this)
.crop(16f, 9f) //Crop image with 16:9 aspect ratio
.start()
Crop square image(e.g for profile)
ImagePicker.with(this)
.cropSquare() //Crop square image, its same as crop(1f, 1f)
.start()
Compress image size(e.g image should be maximum 1 MB)
ImagePicker.with(this)
.compress(1024) //Final image size will be less than 1 MB
.start()
Set Resize image resolution
ImagePicker.with(this)
.maxResultSize(620, 620) //Final image resolution will be less than 620 x 620
.start()
Intercept ImageProvider, Can be used for analytics
ImagePicker.with(this)
.setImageProviderInterceptor { imageProvider -> //Intercept ImageProvider
Log.d("ImagePicker", "Selected ImageProvider: "+imageProvider.name)
}
.start()
Intercept Dialog dismiss event
ImagePicker.with(this)
.setDismissListener {
// Handle dismiss event
Log.d("ImagePicker", "onDismiss");
}
.start()
Specify Directory to store captured, cropped or compressed images. Do not use external public storage directory (i.e. Environment.getExternalStorageDirectory())
ImagePicker.with(this)
/// Provide directory path to save images, Added example saveDir method. You can choose directory as per your need.
// Path: /storage/sdcard0/Android/data/package/files
.saveDir(getExternalFilesDir(null)!!)
// Path: /storage/sdcard0/Android/data/package/files/DCIM
.saveDir(getExternalFilesDir(Environment.DIRECTORY_DCIM)!!)
// Path: /storage/sdcard0/Android/data/package/files/Download
.saveDir(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!)
// Path: /storage/sdcard0/Android/data/package/files/Pictures
.saveDir(getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!)
// Path: /storage/sdcard0/Android/data/package/files/Pictures/ImagePicker
.saveDir(File(getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!, "ImagePicker"))
// Path: /storage/sdcard0/Android/data/package/files/ImagePicker
.saveDir(getExternalFilesDir("ImagePicker")!!)
// Path: /storage/sdcard0/Android/data/package/cache/ImagePicker
.saveDir(File(getExternalCacheDir(), "ImagePicker"))
// Path: /data/data/package/cache/ImagePicker
.saveDir(File(getCacheDir(), "ImagePicker"))
// Path: /data/data/package/files/ImagePicker
.saveDir(File(getFilesDir(), "ImagePicker"))
// Below saveDir path will not work, So do not use it
// Path: /storage/sdcard0/DCIM
// .saveDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM))
// Path: /storage/sdcard0/Pictures
// .saveDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))
// Path: /storage/sdcard0/ImagePicker
// .saveDir(File(Environment.getExternalStorageDirectory(), "ImagePicker"))
.start()
Limit MIME types while choosing a gallery image
ImagePicker.with(this)
.galleryMimeTypes( //Exclude gif images
mimeTypes = arrayOf(
"image/png",
"image/jpg",
"image/jpeg"
)
)
.start()
You can also specify the request code with ImagePicker
ImagePicker.with(this)
.maxResultSize(620, 620)
.start(101) //Here 101 is request code, you may use this in onActivityResult
Add Following parameters in your colors.xml file, If you want to customize uCrop Activity.
<resources>
<!-- Here you can add color of your choice -->
<color name="ucrop_color_toolbar">@color/teal_500</color>
<color name="ucrop_color_statusbar">@color/teal_700</color>
<color name="ucrop_color_widget_active">@color/teal_500</color>
</resources>