Image Picker and compression
source link: https://www.tuicool.com/articles/hit/IzMrmmE
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ImagePicker
Android library to choose image from gallery or camera with option to compress result image.
Download
Add this to your project's build.gradle
allprojects { repositories { maven { url "https://jitpack.io" } } }
And add this to your module's build.gradle
dependencies { implementation 'com.github.maayyaannkk:ImagePicker:x.y.z' }
change x.y.z
to version in
Usage
For full example, please refer to app
module
Start image picker activity
The simplest way to start is create 'ImagePicker' class object
ImagePicker imagePicker = new ImagePicker();
Setup options and start the activity
imagePicker.withActivity(this) //calling from activity .withFragment(this) //calling from fragment .chooseFromGallery(false) //default is true .chooseFromCamera(false) //default is true .withCompression(false) //default is true .start();
Receive result
@Override protected void onActivityResult(int requestCode, final int resultCode, Intent data) { if (requestCode == ImagePicker.SELECT_IMAGE && resultCode == Activity.RESULT_OK) { //Add compression listener if withCompression is set to true imagePicker.addOnCompressListener(new ImageCompressionListener() { @Override public void onStart() { } @Override public void onCompressed(String filePath) {//filePath of the compressed image //convert to bitmap easily Bitmap selectedImage = BitmapFactory.decodeFile(filePath); } }); } //call the method 'getImageFilePath(Intent data)' even if compression is set to false String filePath = imagePicker.getImageFilePath(data); if (filePath != null) {//filePath will return null if compression is set to true Bitmap selectedImage = BitmapFactory.decodeFile(filePath); } super.onActivityResult(requestCode, resultCode, data); }
Corner cases
throws IllegalStateException if:
-activity and fragment both are null
-chooseFromCamera and chooseFromGallery both are false
-write external file permission not found i.e request write external permission before calling 'start()' method
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK