---
title: "Code for take your picture from gallery in Android."  
description: "Code for take your picture from gallery in Android."  
author: "Arti Mishra"  
published: 2018-08-22  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/34665/code-for-take-your-picture-from-gallery-in-android  
category: "android apps"  
tags: ["android-imageview"]  
reading_time: 3 minutes  

---

# Code for take your picture from gallery in Android.

How to take your [picture](https://answers.mindstick.com/qa/38554/name-the-oscar-winning-sound-designer-who-became-the-first-asian-to-win-the-award-for-best-sound-for-documentary-india-s-daughter-at-the-coveted-motion-picture-sound-editors-63rd-annual-golden-reel-awards) from the [gallery](https://answers.mindstick.com/qa/31021/how-do-i-style-a-gallery-wall) and [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) in [your android](https://www.mindstick.com/forum/33405/how-to-get-the-build-version-number-of-your-android-application) project.

## Replies

### Reply by Aryan Kumar

Sure, here is the code to take your picture from gallery in Android:

public void pickImageFromGallery() { // Check if the storage permission has been granted if (checkStoragePermission()) { // The permission has already been granted Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 100); } else { // Request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); } }

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);

Code snippet

```plaintext
if (requestCode == 100 && resultCode == RESULT_OK) {
    // Get the image from the gallery
    Uri imageUri = data.getData();

    // Set the image in the image view
    ImageView imageView = findViewById(R.id.imageView);
    imageView.setImageURI(imageUri);
}
```

}

This code will first check if the `WRITE_EXTERNAL_STORAGE` permission has already been granted. If it has not been granted, the code will request the permission from the user. If the user grants the permission, the code will start an activity to allow the user to select an image from the gallery.

The `startActivityForResult()` method has two parameters:

- The intent that will be used to start the activity.
- The request code that will be used to identify the result of the activity.

The activity that is started will return a result code. If the user selects an image from the gallery, the result code will be `RESULT_OK`. The image that the user selected will be returned in the `data` parameter of the `onActivityResult()` method.

In the `onActivityResult()` method, you can set the image in the image view by calling the `setImageURI()` method of the image view.

To use this code, you would need to call the `pickImageFromGallery()` method in your app. For example, you could call the method in your app's `onCreate()` method.

Here is an example of how you could call the `pickImageFromGallery()` method in your app's `onCreate()` method:

Code snippet

```plaintext
public void onCreate() {
    super.onCreate();

    // Create an image view to display the image
    imageView = findViewById(R.id.imageView);

    // Set a listener to pick an image from the gallery
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pickImageFromGallery();
        }
    });
}
```

When you run the app, you will see an image view. You can click on the image view to pick an image from the gallery.

### Reply by Arti Mishra

## /* Create object of ImageView */

ImageView editImage = (ImageView) findViewById(R.id.edit_image);

## /* Take your picture from gallery */

editImage.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent gallery = new Intent(Intent.ACTION_PICK, [android](https://www.mindstick.com/articles/13046/10-reasons-why-i-prefer-android-vs-iphone-ios).provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

startActivityForResult(gallery, PICK_IMAGE);

}

});


---

Original Source: https://www.mindstick.com/forum/34665/code-for-take-your-picture-from-gallery-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
