Pre-requisites:
- Android App Development Fundamentals for Beginners
- Guide to Install and Set up Android Studio
- Android | Starting with first app/android project
- Android | Running your first Android app
The purpose of this article is to show how to open Camera from inside an App and click the image and then display this image inside the same app. An android application has been developed in this article to achieve this. The opening of Camera from inside our app is achieved with the help of ACTION_IMAGE_CAPTURE Intent of MediaStore class.
For Example:
This image shows the Image clicked by the camera and set in Imageview. When the app is opened, it displays the “Camera” Button to open the camera. When pressed, ACTION_IMAGE_CAPTURE Intent gets started by the MediaStore class. When the image is captured, it is displayed in the imageview.
How to create an App to open Camera through Intent and display captured image
Step 1: Firstly create a new Android Application. This will create an XML file “activity_main.xml” and a Java File “MainActivity.Java”. Please refer to the pre-requisites to learn more about this step.
Step 2: Open “activity_main.xml” file and add following widgets in a Relative Layout:
- A Button to open the Camera
- An ImageView to display the captured image
Also, Assign the ID to each component along with other attributes as shown in the image and the code below. The assigned ID on a component helps that component to be easily found and used in the Java files.
Syntax:
android:id="@+id/id_name"
Here the given IDs are as follows:
- Camera Button: camera_button
- ImageView: click_image
This step will make the UI of the Application.
Step 3: Now, after the UI, this step will create the Backend of the App. For this, open the “MainActivity.java” file and instantiate the components made in the XML file (Camera Button, ImageView) using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
General Syntax:
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
Syntax for components used:
Button camera_open_id= (Button)findViewById(R.id.camera_button);
ImageView click_image_id = (ImageView)findViewById(R.id.click_image );
Step 4: This step involves setting up the operations on the Camera Button and ImageView. These operations are as follows:
- First define the variable pic_id that is request id of clicked image. This is done as follows:
private static final int pic_id = 123
- Add the listener on the Camera button. This will be used to open the camera when the user clicks on the button. This is done as follows:
camera_open_id.setOnClickListener(new View.OnClickListener() {}
- Now create the ACTION_IMAGE_CAPTURE Intent provided by MediaStore. This Intent will help to open the camera for capturing the image. Start the intent with the requested pic_id. This is done as follows:
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera_intent, pic_id);
- Now use onActivityResult() method to get the result, here the captured image. This is done as follows:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { }
- Then set the image received as result of Camera intent in the ImageView for display.
Bitmap photo = (Bitmap) data.getExtras().get(“data”);
clicked_image_id.setImageBitmap(photo);
Step 5: Now Run the app and operate as follows:
- When the app is opened, it displays a “Camera” Button. Click on it to open the Camera.
- Capture the image and display the captured image.
Complete code of MainActivity.java and activity_main.xml of Camera application is below.
Filename: activity_main.xml
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:app
=
"http://schemas.android.com/apk/res-auto"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
Button
android:layout_width
=
"100dp"
android:layout_height
=
"50dp"
android:layout_marginLeft
=
"140dp"
android:id
=
"@+id/camera_button"
android:text
=
"Camera"
/>
<
ImageView
android:layout_marginTop
=
"70dp"
android:layout_width
=
"350dp"
android:layout_marginLeft
=
"15dp"
android:layout_height
=
"450dp"
android:id
=
"@+id/click_image"
android:layout_marginBottom
=
"10dp"
/>
</
RelativeLayout
>
Filename: MainActivity.java
Java
package
org.geeksforgeeks.navedmalik.camera;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.provider.MediaStore;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.ImageView;
public
class
MainActivity
extends
AppCompatActivity {
private
static
final
int
pic_id =
123
;
Button camera_open_id;
ImageView click_image_id;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera_open_id = (Button)findViewById(R.id.camera_button);
click_image_id = (ImageView)findViewById(R.id.click_image);
camera_open_id.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v)
{
Intent camera_intent
=
new
Intent(MediaStore
.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera_intent, pic_id);
}
});
}
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
Intent data)
{
if
(requestCode == pic_id) {
Bitmap photo = (Bitmap)data.getExtras()
.get(
"data"
);
click_image_id.setImageBitmap(photo);
}
}
}
Output: