Pick Image From Gallery Or Camera In Android Studio Programmatically

Hello, and welcome to pick image from gallery or camera in android studio example.

Pick image from gallery or camera in android tutorial guides you how to select/get the image from a gallery in android programmatically.

We will choose/take a photo from gallery or camera in the Android Studio by opening via Intent.

After getting image from gallery or camera, we will show it in an ImageView.

First, check the output of pick image from gallery or camera in android example and then we will develop it.

How to get maximum benefit from pick image from gallery or camera in android studio example

Download Source Code

Creating Pick Image From Gallery Or Camera In Android Studio Tutorial step by step

Step 1: Create a new project in Android Studio.

Step 2: Updating AndroidManifest.xml file

 add required permissions between <manifest>….</manifest> tag.

Note: If you are targeting SDK version above 22 (Above Lollipop)  then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.

Final code for AndroidManifest.xml file

Step 3: Updating activity_main.xml file

 Copy and paste below source code in activity_main.xml file

Step 4: Preparing MainActivity.java class

Add following source code in MainActivity.java class

Step 5: Description of MainActivity.java

When the user clicks the button, dialog with select options appears.

Following is button click method.

The dialog contains options whether to select an image from gallery or to capture from camera.

Below is the code for showPictureDialog() method.

If a user selects gallery, then following method is executed.

And if user choose camera, then below method is run.

After selecting an image from gallery or capturing photo from camera, an onActivityResult() method is executed.

Code for onActivityResult(), is as below.

If an image comes from an gallery, then compiler goes at below code.

If a photo is from a camera, then compiler goes to following.

Below is the method to save the image or photo.

In above code, IMAGE_DIRECTORY is the folder name in which all the images will be saved.

Kotlin Version

Kotlin Version of this tutorial : Android Kotlin Get Image from Gallery

Also Read

How to get exact Android example while searching in Google

Click to get Important Google searching tips

So that’s all for pick image from gallery or camera in android example. If you have queries, then ask them in the comment section. Thank you 🙂

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

14 Responses

  1. Stella says:

    Hi, I have a question.Where I need to create the folder IMAGE_DIRECTORY so the app will not give me an error?

    • Hardik Parsania says:

      You don’t need to make any folder manually. It will be generated automatically. Check first few lines of saveImage(Bitmap myBitmap) function.

  2. Andile Simelane says:


    Thanks for the tutorial,

    Can we expand it by uploading/retrieving the selected image to mysql and php server

  3. np says:

    Hi ,
    it is not supporting for nougat version so please give me suggestion ,how it will for nougat

  4. np1 says:

    Hi ,
    it is not supporting for nougat version so please give me suggestion ,how it will for nougat

  5. abhijeet says:

    can you help me that i have to create sketch yourself app.

  6. pepe says:

    Bro can you explain what is IMAGE_DIRECTORY and how to get the permissions for camera?

  7. Eduardo Teixeira says:

    Hi, great post.
    I have a doubt, the IMAGE_DIRECTORY is the image path?

  8. Iwan says:

    Hi, just want to ask if the image is fullsized ?

  9. Thanaeswar says:

    Thanks sir its work

  10. Vincent Calzado says:

    can i ask question.. how would i get the full quality of the image?

  11. Adrian F says:

    Great tutorial Hardik. Thanks! For those asking about IMAGE_DIRECTORY, Hardik’s code creates this directory in external storage (eg sd card), and you can rename by setting IMAGE_DIRECTORY to whatever you want, eg “MyApp”. For runtime permissions to run camera, try this link https://stackoverflow.com/questions/33666071/android-marshmallow-request-permission.

Leave a Reply

Your email address will not be published. Required fields are marked *