Home

Awesome

📸Image Picker Library for Android

Download Releases API Build Status Language Android Arsenal ktlint PRWelcome Open Source Love License Twitter

<div align="center"> <sub>Built with ❤︎ by <a href="https://twitter.com/Dhaval2404">Dhaval Patel</a> and <a href="https://github.com/dhaval2404/imagepicker/graphs/contributors"> contributors </a> </div> <br/>

Easy to use and configurable library to Pick an image from the Gallery or Capture image using Camera. It also allows to Crop and Compresses the Image based on Aspect Ratio, Resolution and Image Size.

Almost 90% of the app that I have developed has an Image upload feature. Along with the image selection, Sometimes I needed a crop feature for profile image for that I've used uCrop. Most of the time I need to compress the image as the image captured from the camera is more than 5-10 MBs and sometimes we have a requirement to upload images with specific resolution/size, in that case, image compress is the way to go option. To simplify the image pick/capture option I have created ImagePicker library. I hope it will be useful to all.

🐱‍🏍Features:

🎬Preview

Profile Image PickerGallery OnlyCamera Only

💻Usage

  1. 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'
    
  2. 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

    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()
    
  3. 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

💥Compatibility

✔️Changelog

Version: 2.1

Version: 2.0

Version: 1.8

Version: 1.7

Version: 1.6

Version: 1.5

Version: 1.4

Version: 1.3

Version: 1.2

Version: 1.1

Version: 1.0

📃 Libraries Used

Let us know!

We'll be really happy if you sent us links to your projects where you use our component. Just send an email to dhavalpatel244@gmail.com And do let us know if you have any questions or suggestion regarding the library.

License

Copyright 2019-2021, Dhaval Patel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.