13 November 2014

Make A Simple Camera App in Android

What we will do?
-> We will make an application with a button which when pressed opens a camera. You can take a picture then it is saved in the sdcard.

What we need?
  • Eclipse ADT
  • A mobile with a Camera and sdcard
Step 1 : Create a new Project by going on File->New->Android Application Project.

Step 2 : Give a Name to your Application like CameraApp. Then click on Next...till Finish.
 

Step 3: Make a Button in activity_main.xml found in res->layout folder.
activity_main.xml
<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"
tools:context=".MainActivity"
android:background="@drawable/ic_launcher">"

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Press to Open Camera"
android:background="#FFFFFF"></Button>"


</RelativeLayout>

Step 4: Open MainActivity.java found in src folder and write the code below.

MainActivity.java
package com.mia.cameraapp;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

    public File imageFile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"img1.jpg");
        Uri u = Uri.fromFile(imageFile);
        i.putExtra(MediaStore.EXTRA_OUTPUT, u);
        i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(i,0);
    }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if(requestCode ==0)
            {
                switch(resultCode)
                {
                case Activity.RESULT_OK :
                    if(imageFile.exists())
                        Toast.makeText(this, "Image Saved at"+imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED :Toast.makeText(this, "Image Not Saved", Toast.LENGTH_LONG).show();
                    break;
                default : break;
                }
            }        
    }

}
Step 5: Run the Project in your Mobile phone directly since Camera will only be available in your phone. The Webcam cannot take the Camera functionality.

Explanation of the code:
  • A simple button is made in activity_main.xml.
  • In MainActivity class a File object is made named imageFile.
  • Intent i is defined to carry out action of opening the camera and MediaStore is used to set action of capturing photo.
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  • MediaStore is an inbuild class which deals with storing media like images,video etc.
imageFile = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),"img1.jpg");
  • Environment class lets us to access the sd card and store the image in it inside PICTURES directory naming the image to img.jpg.
  • The Uri object will get the url of the image location and make it into a string (e.g. "sdcard/Pictures/special/").
i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
  • EXTRA_VIDEO_QUALITY decides the quality of image (1- high quality, 0-low quality).
  • Then we start the Activity by using startActivity() with parameter as Intent object (i) and requestCode (0).
startActivityForResult(i,0);
  • Then we decide what to do after Activity has started by overriding method onActivityResult().
protected void onActivityResult(int requestCode, int resultCode, Intent data)
  • Then we check the condition if our requestCode matches the Activity(if loaded Activity is img1 only and not any other one).
  • We have a switch case in it which has two cases (if result is OK / result is not OK / default).
if(requestCode ==0)
            {
                switch(resultCode)
                {
                case Activity.RESULT_OK :
                    if(imageFile.exists())
                        Toast.makeText(this, "Image Saved at"+imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED :Toast.makeText(this, "Image Not Saved", Toast.LENGTH_LONG).show();
                    break;
                default : break;
                }
            }
  • If result is Ok we make a Toast which returns path of image where it is saved.
  • If result is not Ok it shows error message in a Toast.

Stay Tuned with Made In Android

Previous Page Next Page Home

No comments:

Post a Comment

Top