Posted on

Android Better Image Picker Library


andorid simplest image picker library

 

Hi, with Android 6.0 runtime permissions and 7.0 FileUriExpossed for camera it’s got more complex to get pictures from camera/gallery. Luckily we’ve got many great libraries that provide great abstaction. One of them is Android Image Cropper. Let’s make it better.

Besides just picking images from gallery/camera you can crop it with desired aspect ratio. That one is very important, if you noticed every app up there like Facebook, Google etc asks you to crop image into square because it fits the best for different use cases.

Although it’s such a great library, there’s still too much code you need to write, especially with permissions. You can implement them pretty easy with libraries like Permission Dispatcher, but still, you need to think of it. Also handling failures, all this makes picking pictures pretty cluttered.

Make It Better

What do you think of this approach?

That’s basically it. When you call choosePicture method you see this intent selector

andorid simplest image picker library

And I also added boolean includeCameraIntents as attribute for this method if you decide not to show camera intent

For example if you have your own camera activity and want this one to be just a gallery picker.

And if you want to open camera directly, you can with this method

It will open device’s default camera app.

Handle Results

Even though we trying to keep it simple, your activity is still the place of entry for permission and activity results. Which means that we have to delegate activity results (image picked) and permissions to image picker. Override those methods in your activity.

imagePicker instance should be saved as a filed variable.

Now if you’re calling it from fragment, not activity you need to override those two methods above in you fragment instead. One thing is that onActivityResult will be public. But fragments don’t get onActivityResult callback automatically, so we have to call it manually in our activity. Override this method in your activity

Or you can access your fragment instance by any other of the possible ways

Also when creating ImagePicker instance don’t forget to use this as second argument and getActivity() as first.

Crop Image

Now since we have a library that knows how to crop images under the hood, why not use it. When creating image picker instance, you can set it to crop images after you picked them

By default you don’t crop images and here you declare aspect ratio that you want to crop to. Square is the most common.

andorid simplest image picker library

Retrieve File

You get file uri in the callback you declared during creating ImagePicker instance. That’s a uri to picked/cropped image. But if you want to access file itself, you can do it with this method

Finally, we need to add this library to dependencies, of course.

 

By the way, if you want to learn how to build and upload your own libraries to jCenter – check out this post with all the details that you need to know

 

I hope you’ll find this library useful and easy to use. You can get sample app and library itself here. Don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends

 

 

 

 

 


  • Ciprian Grigor

    Nice, do you have a public repo?

  • Aqit Malinzda

    please help me get the uri picture

  • Aqit Malinzda

    please help me get the uri picture
    i tried this but failed

    imagePicker = new ImagePicker(this /*activity non null*/,
    null /*fragment nullable*/,
    imageUri -> { /*on image picked*/
    imgThumb.setImageURI(imageUri);
    uri = imageUri;
    });