Problem: Launch native camera application to capture image and use it inside imageview
Description:
I hope you may across the application in which they are launching camera to capture image and then
they use the captured image to show inside the imageview. So here is an article for you to
implement the Camera functionality to capture photo and then place the captured photo inside
the imageview.
Prerequisites to read this article:
1.android.provider.MediaStore.ACTION_IMAGE_CAPTURE
By using this intent, we can launch the camera and can capture the photo/snap.
2.startActivityForResult
Usually we start activity by using startActivity method, but here we will start the activity
by using startActivityResult method as we require result as captured image. In short, this will launch an Activity that provides a result, and then pass that result back to the Activity that launched it.
Note: It is not required to have the android.permission.CAMERA permission in the AndroidManifest.xml file as we are not accessing the CAMERA directly, but we are just launching the in-built camera application through the above mentioed intent (i.e. android.provider.MediaStore.ACTION_IMAGE_CAPTURE).
We can also speak about this question as “How to capture image using Camera and use it inside the android application?”.
Well, here is the Activity code and xml layout files, just create them and run your application.
CameraActivity.java
package com.paresh.cameracapturedemo; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { /** Called when the activity is first created. */ Button btnTakePhoto; ImageView imgTakenPhoto; private static final int CAM_REQUREST = 1313; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnTakePhoto = (Button) findViewById(R.id.button1); imgTakenPhoto = (ImageView) findViewById(R.id.imageView1); btnTakePhoto.setOnClickListener(new btnTakePhotoClicker()); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA_PIC_REQUEST) { Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); imgTakenPhoto.setImageBitmap(thumbnail); } } class btnTakePhotoClicker implements Button.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); } } }
————————————————————————————-
main.xml
————————————————————————————-
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Press the button, capture photo and enjoy!!" android:id="@+id/textview1"/> <Button android:text="Take Photo!" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview1"> </Button> <ImageView android:id="@+id/imageView1" android:layout_height="match_parent" android:layout_width="match_parent" android:src="@drawable/icon" android:scaleType="centerCrop" android:layout_below="@+id/button1"> </ImageView> </RelativeLayout>