/ Images

Pick image from Gallery or Capture new image with Camera

Pick image from Gallery or Capture new image with Camera

Image Picker Library for Android

Simple Library to Pick an image from the Gallery or Capture image with Camera.

Almost 90% of the app that I have developed has Image upload feature. To simplify the image pick/capture option I have created this library. Its easily configurable and easy to use.


  1. Include the library as local library project.

    allprojects {
       repositories {
    implementation 'com.github.dhaval2404:imagepicker:1.0'
  2. The ImagePicker configuration is created using the builder pattern.

          .crop(1f, 1f)	    		//Crop Square image(Optional)
    	  .compress(1024)			//Final image size will be less than 1 MB(Optional)
          .maxResultSize(620, 620)	//Final image resolution will be less than 620 x 620(Optional)
  3. 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
            //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()


  • Pick image using Gallery

    	    .galleryOnly()       //User can only select image from Gallery
    	    .start()			 //Default Request Code is ImagePicker.REQUEST_CODE
  • Capture image using Camera

    	    .cameraOnly()       //User can only capture image using Camera
  • Crop image

    	    .crop(16f, 9f)	   //Crop image with 16:9 aspect ratio
  • Crop square image(e.g for profile)

           .cropSquare()	   //Crop square image, its same as crop(1f, 1f)
  • Compress image size(e.g image should be maximum 1 MB)

    	    .compress(1024)	   //Final image size will be less than 1 MB
  • Set Resize image resolution

        	//Final image resolution will be less than 620 x 620
    	    .maxResultSize(620, 620)	   
  • You can also specify the request code with ImagePicker

    	    .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.

        <!-- 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>
  • You don't need to add any permissions to manifest, everything is merged automatically from library's manifest file. You can remove unnecessary permission by adding tools:node="remove tag.

       If Not using Camera feature, Add following line in app manifest.
       This will remove permission while manifest merge
       <uses-permission android:name="android.permission.CAMERA" tools:node="remove"/> 


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