<p>In this tutorial i will demonstrate you how to capture an image from camera and pick an image from gallery and then display it in an imageView by creating a simple <strong><span style="color: #0000ff;">ImagePickerApp</span></strong>.</p>
<p> ;</p>
<p><img class="aligncenter wp-image-760 size-full" src="https://c1ctech.com/wp-content/uploads/2018/08/Screenshot_2018-08-21-15-40-291.png" alt="" width="480" height="800" /></p>
<p> ;</p>
<h3><span style="color: #000080;">Create new project</span></h3>
<p><strong>1</strong>. Create a new project in <span style="color: #008000;"><strong>Android Studio</strong></span> from <span style="color: #008000;"><strong>File ⇒ New Project</strong></span> by filling the required details. When it prompts you to select the activity, choose <span style="color: #008000;"><strong>Empty Activity</strong></span> and continue.</p>
<p><strong>2</strong>. Open <strong><span style="color: #008000;">AndroidManifest.xml</span></strong> and add the following permissions :</p>
<pre><code><;uses-feature
 android:name="android.hardware.camera"
 android:required="true" />;</code></pre>
<p>To advertise that your application depends on having a camera, put a <span style="color: #008000;"><strong><;uses-feature>; </strong></span>tag of <strong><span style="color: #008000;">name=&#8221;android.hardware.camera&#8221;</span> </strong>in your manifest file.By adding <span style="color: #008000;"><strong>android.hardware.camera</strong></span>, Play Store detects and prevents installing the application on devices with no camera.</p>
<p>By setting <span style="color: #008000;"><strong>android:required</strong></span> to <strong><span style="color: #008000;">false</span></strong> ,Google Play will allow devices without a camera to download your application.</p>
<p><strong>3</strong>. Open <span style="color: #008000;"><strong>activity_main.xml</strong> </span>and write the below code.It contains one <span style="color: #0000ff;"><strong>imageView</strong></span> and two buttons <strong><span style="color: #0000ff;">Camera</span></strong> and <strong><span style="color: #0000ff;">Gallery</span></strong> ,<strong>btn_camera</strong> is to capture image from camera and <strong>btn_gallery</strong> is to pick image from gallery.</p>
<p><span style="color: #0000ff;"><strong>activity_main.xml</strong></span></p>
<pre><code><;?xml version="1.0" encoding="utf-8"?>;
<;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_margin="10dp"
 android:orientation="vertical"

 >;

 <;ImageView
 android:id="@+id/image"
 android:layout_width="250dp"
 android:layout_height="250dp"
 android:layout_gravity="center"
 android:layout_marginTop="10dp"
 android:background="@drawable/background"
 android:scaleType="centerCrop" />;

 <;LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginTop="20dp"
 android:orientation="horizontal">;

 <;Button
 android:id="@+id/btn_camera"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginRight="8dp"
 android:layout_weight="1"
 android:background="@color/colorPrimary"
 android:text="camera"
 android:textColor="@android:color/white"
 android:textSize="18sp"
 android:textStyle="bold" />;

 <;Button
 android:id="@+id/btn_gallery"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginLeft="8dp"
 android:layout_weight="1"
 android:background="@color/colorPrimary"
 android:text="gallery"
 android:textColor="@android:color/white"
 android:textSize="18sp"
 android:textStyle="bold" />;


 <;/LinearLayout>;

<;/LinearLayout>;</code></pre>
<p>4. Open <strong><span style="color: #008000;">MainActivity.java</span></strong> and write the below code:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p><span style="color: #0000ff;"><strong>MainActivity.java</strong></span></p>
<pre>package com.example.cameratest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

 ImageView imageView;
 Button btn_camera, btn_gallery;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 imageView = (ImageView) findViewById(R.id.image);
 btn_camera = (Button) findViewById(R.id.btn_camera);
 btn_gallery = (Button) findViewById(R.id.btn_gallery);


 btn_camera.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 //To take picture from camera

 Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(takePicture, 0);//zero can be replaced with any action code

 }
 });
 btn_gallery.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 //To pick photo from gallery

 Intent pickPhoto = new Intent(Intent.ACTION_PICK,
 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 startActivityForResult(pickPhoto, 1);//one can be replaced with any action code
 }
 });

 }

 protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
 super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
 switch (requestCode) {
 case 0:
 if (resultCode == RESULT_OK) {
 Bundle extras = imageReturnedIntent.getExtras();
 Bitmap imageBitmap = (Bitmap) extras.get("data");
 imageView.setImageBitmap(imageBitmap);
 }

 break;
 case 1:
 if (resultCode == RESULT_OK) {
 Uri selectedImage = imageReturnedIntent.getData();
 imageView.setImageURI(selectedImage);
 }
 break;
 }
 }
}</pre>
<p>Intent is the standard way to delegate actions to another application.</p>
<ul>
<li>To start the native camera the Intent requires <span style="color: #008000;"><strong>android.provider.MediaStore.ACTION_IMAGE_CAPTURE</strong></span>.</li>
<li>To pick an image from gallery, the Intent requires the following argument : <span style="color: #008000;"><strong>Intent.ACTION_PICK</strong></span>.</li>
</ul>
<p>Now we know how to capture and pick image from camera and gallery now its time to set image in an <strong>imageView</strong>.</p>
<p><strong><span style="color: #008000;">startActivityForResult</span></strong>() contains intent and related request code returns an intent to <span style="color: #008000;"><strong>onActivityResult</strong></span>() .Inside onActivityResult() we get the Uri from the Intent and set it in <span style="color: #008000;"><strong>imageView</strong></span> as shown below:</p>
<pre><code>Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);</code></pre>
<p> ;</p>
<p><span style="color: #0000ff;"><strong>When you run your app it will look like this as shown below:</strong></span></p>
<p><img class="alignnone size-medium wp-image-759" src="https://c1ctech.com/wp-content/uploads/2018/08/Screenshot_2018-08-21-15-35-431-180x300.png" alt="" width="180" height="300" /> <img class="alignnone size-medium wp-image-760" src="https://c1ctech.com/wp-content/uploads/2018/08/Screenshot_2018-08-21-15-40-291-180x300.png" alt="" width="180" height="300" /></p>
<p>I hope this post is helpful for you in understanding how to <strong><span style="color: #0000ff;">Capture Image From Camera and Select Image From Gallery</span></strong> in an android application.

