├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── in │ │ └── mayanknagwanshi │ │ └── imagepicker │ │ └── demo │ │ ├── ExampleActivity.java │ │ └── MainActivity.java │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_example.xml │ ├── activity_example_fragment.xml │ ├── activity_main.xml │ └── fragment_example.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── imagepicker ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── in │ │ └── mayanknagwanshi │ │ └── imagepicker │ │ ├── ImageCropActivity.java │ │ ├── ImageSelectActivity.java │ │ ├── imageCompression │ │ ├── ImageCompression.java │ │ └── ImageCompressionListener.java │ │ ├── imagePicker │ │ └── ImagePickerUtil.java │ │ ├── provider │ │ └── ImageSelectionProvider.java │ │ └── view │ │ └── ImageCropView.java │ └── res │ ├── drawable-nodpi │ └── frame.png │ ├── layout │ ├── activity_image_crop.xml │ └── activity_image_select.xml │ ├── values │ └── strings.xml │ └── xml │ └── provider_paths.xml ├── jitpack.yml ├── sample.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Mayank Nagwanshi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ImagePicker ![Downloads](https://jitpack.io/v/maayyaannkk/ImagePicker/month.svg) 2 | Android library to choose image from gallery or camera with option to compress result image. 3 | 4 | # Download [![](https://jitpack.io/v/maayyaannkk/ImagePicker.svg)](https://jitpack.io/#maayyaannkk/ImagePicker) [![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-Image%20Picker%20and%20compression-green.svg?style=flat )]( https://android-arsenal.com/details/1/7055 ) 5 | 6 | Add this to your project's `build.gradle` 7 | 8 | ```groovy 9 | allprojects { 10 | repositories { 11 | maven { url "https://jitpack.io" } 12 | } 13 | } 14 | ``` 15 | 16 | And add this to your module's `build.gradle` 17 | 18 | ```groovy 19 | dependencies { 20 | implementation 'com.github.maayyaannkk:ImagePicker:x.y.z' 21 | } 22 | ``` 23 | 24 | change `x.y.z` to version in [![](https://jitpack.io/v/maayyaannkk/ImagePicker.svg)](https://jitpack.io/#maayyaannkk/ImagePicker) 25 | 26 | ## Usage 27 | 28 | For full example, please refer to `app` module 29 | 30 | No need to request for write external storage permission, library will do that. 31 | ### Crop with 1:1 aspect ratio 32 | 33 | 34 | ### Start image picker activity 35 | 36 | The simplest way to start is setup options and start the activity. Set the FLAG_CROP to crop resulting image in 1:1 aspect ratio 37 | ```java 38 | Intent intent = new Intent(this, ImageSelectActivity.class); 39 | intent.putExtra(ImageSelectActivity.FLAG_COMPRESS, false);//default is true 40 | intent.putExtra(ImageSelectActivity.FLAG_CAMERA, true);//default is true 41 | intent.putExtra(ImageSelectActivity.FLAG_GALLERY, true);//default is true 42 | intent.putExtra(ImageSelectActivity.FLAG_CROP, isCrop);//default is false 43 | startActivityForResult(intent, 1213); 44 | ``` 45 | Receive result 46 | ```java 47 | @Override 48 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 49 | super.onActivityResult(requestCode, resultCode, data); 50 | if (requestCode == 1213 && resultCode == Activity.RESULT_OK) { 51 | String filePath = data.getStringExtra(ImageSelectActivity.RESULT_FILE_PATH); 52 | Bitmap selectedImage = BitmapFactory.decodeFile(filePath); 53 | imageView.setImageBitmap(selectedImage); 54 | } 55 | } 56 | ``` 57 | ## Corner cases 58 | throws IllegalStateException if: 59 | -chooseFromCamera and chooseFromGallery both are false 60 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdk 33 5 | buildFeatures.buildConfig true 6 | namespace "in.mayanknagwanshi.imagepicker.demo" 7 | defaultConfig { 8 | applicationId "in.mayanknagwanshi.imagepicker.demo" 9 | minSdkVersion 19 10 | targetSdkVersion 32 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | implementation project(':imagepicker') 25 | implementation 'androidx.appcompat:appcompat:1.3.0' 26 | //testImplementation 'junit:junit:4.12' 27 | //androidTestImplementation 'com.android.support.test:runner:1.0.1' 28 | //androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 14 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/in/mayanknagwanshi/imagepicker/demo/ExampleActivity.java: -------------------------------------------------------------------------------- 1 | package in.mayanknagwanshi.imagepicker.demo; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.os.Bundle; 8 | import android.widget.ImageView; 9 | 10 | import androidx.appcompat.app.AppCompatActivity; 11 | 12 | import in.mayanknagwanshi.imagepicker.ImageSelectActivity; 13 | 14 | public class ExampleActivity extends AppCompatActivity { 15 | private ImageView imageView; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_example); 21 | 22 | imageView = findViewById(R.id.imageView); 23 | ImageSelectActivity.startImageSelectionForResult(this, true, true, true, true, 1213); 24 | } 25 | 26 | @Override 27 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 28 | super.onActivityResult(requestCode, resultCode, data); 29 | if (requestCode == 1213 && resultCode == Activity.RESULT_OK) { 30 | String filePath = data.getStringExtra(ImageSelectActivity.RESULT_FILE_PATH); 31 | Bitmap selectedImage = BitmapFactory.decodeFile(filePath); 32 | imageView.setImageBitmap(selectedImage); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/in/mayanknagwanshi/imagepicker/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package in.mayanknagwanshi.imagepicker.demo; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Button; 7 | 8 | import androidx.appcompat.app.AppCompatActivity; 9 | 10 | public class MainActivity extends AppCompatActivity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_main); 16 | 17 | Button buttonActivity = findViewById(R.id.buttonActivity); 18 | 19 | buttonActivity.setOnClickListener(new View.OnClickListener() { 20 | @Override 21 | public void onClick(View view) { 22 | startActivity(new Intent(MainActivity.this, ExampleActivity.class)); 23 | } 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_example.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_example_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 |