Capturing Image and Video from Camera-Android

The Android framework includes support for various cameras and camera features available on devices, allowing you capturing pictures and videos in your applications.

This example helps you make your own application to capture images and videos and save it to your gallery.

NOTE :  This project is developed in Android Studio, and tested in Android Marshmallow 6.0 .

1. Android Layout Files

In your .xml file whose path is ” res/layout/activity_main.xml ” , edit the following UI components or you can choose yourself.

File : activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.cloudinfinity.camera.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/imageButton"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="140dp"
        android:layout_height="100dp"
        android:id="@+id/imageButton"
        android:src="@drawable/camera"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="140dp"
        android:layout_height="100dp"
        android:id="@+id/imageButton2"
        android:src="@drawable/video"
        android:layout_alignBottom="@+id/imageButton"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/imageView" />
</RelativeLayout>

 

2. Activity Code (Camera Activity)

File : MainActivity.java

package com.example.cloudinfinity.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.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ImageButton b1,b2;
    ImageView iv;
    private static final int VIDEO_REQUEST = 200;
    private static final int CAMERA_REQUEST = 123;

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

        b1=(ImageButton)findViewById(R.id.imageButton);
        b2=(ImageButton)findViewById(R.id.imageButton2);
        iv=(ImageView)findViewById(R.id.imageView);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i,CAMERA_REQUEST);
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(i,VIDEO_REQUEST);
            }
        });
    }

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

        if(requestCode==CAMERA_REQUEST && resultCode==RESULT_OK)
        {
            Bitmap bitmap=(Bitmap)data.getExtras().get("data");
            iv.setImageBitmap(bitmap);
            Toast.makeText(this,"Image Saved in Gallery",Toast.LENGTH_SHORT).show();
        }

        if(requestCode==VIDEO_REQUEST && resultCode==RESULT_OK)
        {
            Toast.makeText(this,"Video Saved in Gallery",Toast.LENGTH_SHORT).show();
        }
    }
}

 

3. Android Manifest

Application required Camera hardware permission, add below into AndroidManifest.xml.

<uses-feature android:name="android.permission.CAMERA"></uses-feature>

 

4. Run the application

Here, this will look like;

Capturing

 

Leave a Reply

Your email address will not be published. Required fields are marked *

ten + nine =