Captured Image in Android using Camera Intent

We can capture image in android using camera intent.An intent action type of MediaStore.ACTION_VIDEO_CAPTURE or MediaStore.ACTION_IMAGE_CAPTURE can be used to capture videos or images without directly using the Camera object.

Create Android Project with name as "StartCamera" with package name "com.etr.startcamera". Create Activity with name as "CameraActivity" and it will start camera. This is the code for CameraActivity.

package com.etr.startcamera;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CameraActivity extends Activity {
public static final int code = 1337;
Button start_camera;

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

start_camera = (Button) findViewById(R.id.start_camera);
start_camera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
startActivityForResult(new Intent(
MediaStore.ACTION_IMAGE_CAPTURE), code);
}
});
} catch (Exception e) {
Log.v("Camera Exception ", Log.getStackTraceString(e));
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Image Captured", Toast.LENGTH_LONG)
.show();
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera, menu);
return true;
}

}

Paste below code to "activity_camera.xml"

<LinearLayout 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:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/start_camera"
        android:layout_width="wrap_content"
        android:text="Start Camera"
        android:layout_height="wrap_content" />

</LinearLayout>

Paste below code to "AndroidManifest.xml"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.etr.startcamera"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.etr.startcamera.CameraActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

You can download source code here.

Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService