ImagePicker

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.

‍🏍Features

  • Pick Gallery Image
  • Pick Image from Google Drive
  • Capture Camera Image
  • Crop Image(Crop image based on provided aspect ratio or let user choose one)
  • Compress Image(Compress image based on provided resolution and size)
  • Retrive Image Result as File, File Path as String or Uri object
  • Handle Runtime Permission for Camera and Storage

ğŸŽ¬Preview

Profile Image Picker Gallery Only Camera Only
imagepicker_profile_demo imagepicker_gallery_demo.gif imagepicker_camera_demo.gif

💻Usage

  1. Gradle dependency:

    allprojects {
       repositories {
          	jcenter() // For ImagePicker library, this lien is enough. Although, it has been published on jitpack as well
           	maven { url "https://jitpack.io" }  //Make sure to add this in your project for uCrop - an internal library
       }
    }
    
    implementation 'com.github.Drjacky:ImagePicker:$libVersion'
    

    Where libVersion

    If you want to get the activity result inline in a modern way (lambda) install InlineActivityResult library:

    implementation 'com.github.florent37:inline-activity-result-kotlin:1.0.4'
    
  2. If you target Android 10 or higher(targetSdkVersion >= 29), set the value of requestLegacyExternalStorage to true in your app's manifest file:

    <manifest ... >
        <!-- This attribute is "false" by default on apps targeting
             Android 10 or higher. -->
        <application android:requestLegacyExternalStorage="true" ... >
          ...
        </application>
    </manifest>
    
  3. The ImagePicker configuration is created using the builder pattern.

    Kotlin

    ImagePicker.with(this)
            .crop()	    			//Crop image(Optional), Check Customization for more option
            .cropOval()				//Allow dimmed layer to have a circle inside
            .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.Companion.with(this)
            .crop()	    			//Crop image(Optional), Check Customization for more option
            .cropOval()				//Allow dimmed layer to have a circle inside
            .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()
    
  4. Handling results

    Default method(Preferred way)

    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 fileUri = data?.data
             imgProfile.setImageURI(fileUri)
          
            //You can get File object from intent
            val file:File = ImagePicker.getFile(data)!!
           
            //You can also get File Path from intent
            val filePath:String = ImagePicker.getFilePath(data)!!
         } 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 InlineActivityResult library, Only Works with FragmentActivity and AppCompatActivity) (Not to be used with crop. See #32)

    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)
            .start { resultCode, data ->
                if (resultCode == Activity.RESULT_OK) {
                    //Image Uri will not be null for RESULT_OK
                     val fileUri = data?.data
                     imgProfile.setImageURI(fileUri)
                  
                    //You can get File object from intent
                    val file:File = ImagePicker.getFile(data)
                   
                    //You can also get File Path from intent
                    val filePath:String = ImagePicker.getFilePath(data)
                } else if (resultCode == ImagePicker.RESULT_ERROR) {
                    Toast.makeText(context, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(context, "Task Cancelled", Toast.LENGTH_SHORT).show()
                }
            }
    

ğŸŽ¨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()
    
  • Specify Directory to store captured, cropped or compressed images

    ImagePicker.with(this)
        //Provide directory path to save images
        .saveDir(File(Environment.getExternalStorageDirectory(), "ImagePicker"))
        // .saveDir(Environment.getExternalStorageDirectory())
        // .saveDir(getExternalFilesDir(null)!!)
        .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>
    

💥Compatibility

  • Library - Android Kitkat 4.4+ (API 19)
  • Sample - Android Kitkat 4.4+ (API 19)

GitHub