Skip to content

📸Image Picker for Android, Pick image from Gallery or Capture new image with Camera

License

Notifications You must be signed in to change notification settings

benjaminlefevre/ImagePicker

 
 

Repository files navigation

📸Image Picker Library for Android

Download Releases API Language Android Arsenal PRWelcome Say Thanks! Twitter

Built with ❤︎ by Dhaval Patel and contributors

Simple Library to Pick an image from the Gallery or Capture image using Camera. It also allows to Crop and Compress the Image based on resolution and image size.

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.

💻Usage

  1. Include the library as local library project.

    allprojects {
       repositories {
          	jcenter()
           	maven { url "https://jitpack.io" }  //Make sure to add this in your project for uCrop
       }
    }
    implementation 'com.github.dhaval2404:imagepicker:1.2'

    If you are yet to Migrate on AndroidX, Use support build artifact:

    implementation 'com.github.dhaval2404:imagepicker-support:1.1'

    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.1'
  2. The ImagePicker configuration is created using the builder pattern.

    Kotlin

    ImagePicker.with(this)
            .crop(1f, 1f)	    		//Crop Square image(Optional)
            .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(1f, 1f)	    		//Crop Square image(Optional)
            .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

    Default method 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)

    ImagePicker.with(this)
            .crop(1f, 1f)               //Crop Square image(Optional)
            .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(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()
  • 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>    
  • 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"/> 

💥Compatibility

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

✔️Changelog

Version: 1.2

  • Added Support for Inline Activity Result(Special Thanks to soareseneves)
  • Fixed issue #6

Version: 1.1

  • Optimized Compression Logic
  • Replace white screen with transparent one.

Version: 1.0

  • Initial Build

📃 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 [email protected] And do let us know if you have any questions or suggestion regarding the library.

License

Copyright 2019, The Android Open Source Project

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.

About

📸Image Picker for Android, Pick image from Gallery or Capture new image with Camera

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 100.0%