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.

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

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...

31 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.

      • Hamza says:

        private static final String IMAGE_DIRECTORY = “/demonuts”;
        private int GALLERY = 1, CAMERA = 2;

  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.

  12. itzik says:

    great tutorial,easy to understand and easy to implement in my code.
    thank you very much Hardik Parsania

  13. Yudis says:

    Hi, thanks for your tutorial, easy to understand.
    Btw, i want to saved that images to MySql database and delete Image Directory of this APP in storage, can you make that tutorial? Thanks

  14. Yudis says:

    How to saved images without making any directory from storage? or using MySql database, thanks.

  15. Ripal Thakkar says:

    it was so helpful for me thnks..

  16. Manish says:

    Can you do the same if you have android Oreo ? Opening Camera , because it throws null pointer exception when you working with oreo , but for rest of previous versions it works fine.

  17. amol gajare says:

    how can we apply this code for dialog or fragment….

  18. amol gajare says:

    by this code camera does not working….

  19. amol gajare says:

    camera can’t open by this code so,what should i do……?

    • Hardik Parsania says:

      Download my source code and check if it is working for camera or not?
      Ping me back with your results.

  20. haffi cheema says:

    sir i’m having some trouble when i click on camera

Leave a Reply

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