This example shows how can user go to phones gallery, select a image and after that display it on screen. This example can be used in variety of scenarios, from setting profile pictures to setting backgrounds. This example is very similar to Take picture with camera and make a preview with the difference of handling the returned data as image.

ButterKnife library

First we add our favorite ButterKnife library in our build.gradle app file and sync the project:

 

AndroidManifest XML:

We must add write permissions to our AndroidManifest in order to call the gallery in our app:

The Main Activity XML:

In this Activity, there is nothing more than to show the selected image on screen. Because of that we use only one defined ImageView, one descriptive TextView and one Button for calling the gallery:

The Main Activity Java:

Also in the Main Activity we call the gallery on button click by starting an activity by intent for result. The app is starting the gallery and receive the selected image as a result. Whit this kind of intent you can choose from where ever you like in your device.

In on activity result we compare if our request code (TAKE_PICTURE) is used, and by that we get the data as result for taking the photo with our camera:

Of course here we have some run-time permissions for the choosing image (used from Android API 23) and that will be covered in later examples. In other code there is still same as Take picture with camera and make a preview example.

Here is the whole MainActivity:

NOTE: This project is done using AndroidX

You can get the whole project on GitHub BuktopMKD/SelectFromGallery

 

 

 

 

Spread the love

Leave a Reply

Your email address will not be published.