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.

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.

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

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

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.

  • This method creates a dialog with two options.
  • One option is to select image from gallery.
  • Another option is to capture image from 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

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

37 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?
    Thanks

    • 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:

    Hi,

    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:

    HI,
    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?

    Thanks.

  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

Leave a Reply

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