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.

You need to implement this feature when you are creating a sign up page with image of the user.

Other example can be like when you are getting review from the user about specific place. Here, user may want to add images.

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

DemoNuts App

Download our app to simplify your learning process.

Link : https://play.google.com/store/apps/details?id=com.main.demonuts

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

Step 1: Create a new project in Android Studio.

I recommend you to make a separate new android project before you go to second step.

Benefit of doing making fresh new project is that you have empty work space in your main activity so the complexity of the coding lines is decreased.

When making new project, select Empty Activity as a default activity so main activity will not include any prewritten codes.

Step 2: Updating AndroidManifest.xml file

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

Final code for AndroidManifest.xml file


Now add the following lines in the build.gradle (Module:app) file

So the whole source code for build.gradle (Module:app) file looks like the below

This line allow us to use the dexter library.

Using this library, we will ask for the runtime permissions in the easy manner.

Step 3: Updating activity_main.xml file

Copy and paste below source code in activity_main.xml file

I have taken one button and Imageview in the main layout.

On the button click, compiler will open one dialog where there are two options.

One is to pick image from gallery and other is to capture image from camera.

If user selects image from the gallery, image will be shown in the Imageview. And if user captures image from the camera, the captured image will be shown in the ImageView.

Step 4: Preparing MainActivity.java class

Add following source code in MainActivity.java class

Step 5: Description of MainActivity.java

In the onCreate() method, compiler will call the requestMultiplePermissions() method.

Source code for requestMultiplePermissions() is as the following

  • This method will ask for multile runtime permissions using dexter library.
  • We will ask for all permissions at one time.
  • It is better to allow every permission when you are testing this tutorial on your device to avoid unnecessary complexity.
  • When the user clicks the button, dialog with select options appears.
  • Following is the button click method.

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

Below is the code for showPictureDialog() method.

  • This method creates a dialog with two options.
  • One option is to select an image from the  gallery.
  • Another option is to capture image from the camera.

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

  • Above gallery intent will open a new screen which includes all the gallery images.
  • User will select image from this screen.
  • And if user choose camera, then below method is run by compiler.

  • Above code will create a camera intent (a camera preview) So the user can capture image from here.
  • 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 🙂

Download Source Code

Downlaod Source Code For SelectImage

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

49 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

  21. Anjaliraj Baburaj says:

    Respected Sir,

    Can you please help me with the coding of how to store image in Sqlite database?


  22. a.hasan says:

    The image is stored rotated to the right or left side, and also if i capture the image from camera it is shown as a thumbnail. Do you have any insights on this problem ?

  23. Amin says:

    thanks for this great tutorial

  24. Briziapps says:

    thanks man for this, save my time and is the best i see because is easy take some peaces of the code and use, if this code works is works ever or some time i need to change on future?

  25. Abhishek Phalke says:

    sir when i pick image from camera image very low quality what i do sir

  26. yossi says:

    hi, I have a question about how to use in a database?
    I need to my project when I take a picture and I close the app the picture sty and don’t gone

  27. Shaon Acharjee says:

    Hi.Thank u for such a nice tutorial. I want to know a point. How can i get the path of saved or gallery image?

    • Hardik Parsania says:

      There is a below line in saveImage() method.
      Log.d(“TAG”, “File Saved::—>” + f.getAbsolutePath());

      f.getAbsolutePath()); will give you a path of saved image.

  28. huzaifa says:

    Application is crashing when i am clicking on the capture photo from gallery!! my android version is noughat

  29. huzaifa says:

    i want to add crop feature after selecting the image from gallery or after clicking the photo how to do that

  30. nikesh says:

    Can We save the Image Without compressing the Image

  31. Alex says:

    Photo save low quality. why?

Leave a Reply

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