Capture images from camera in Android
Hello friends, In this post, we are going to learn how to capture images from camera in Android and display captured photo in ImageView using Android camera application. Here the User Interface consists of one ImageView and another imageView acting as a Button to trigger the camera. The imageView will be used to launch the camera of your own mobile and the ImageView will hold any captured image from the camera and store it in your gallery itself.

Now Lets start with the coding by adding some permissions in the Manifest.xml file.
Below is the permission for the Camera and SD Card Accessing.
1 2 |
<uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
Now, Open the layout file of your main activity activity_main.xml and paste the following code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/ivCapturePicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/Picture" android:layout_marginBottom="10dp"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Camera" android:padding="10dp" android:textSize="15dp"/> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> |
Fine, let us register both the ImageView object and add an onClickListener to the imageView acting as a button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class MainActivity extends AppCompatActivity { ImageView im,show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); im = (ImageView)findViewById(R.id.ivCapturePicture); show=(ImageView)findViewById(R.id.img); im.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePicture, 0); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 0: if(resultCode == RESULT_OK){ selectedImage = imageReturnedIntent.getData(); show.setImageURI(selectedImage); Toast.makeText(getApplicationContext(),""+selectedImage,Toast.LENGTH_LONG).show(); } } } } |
That’s all for the post, now install the application in your android phone and click awesome pictures with your own app. 😉