Android Camera API Example

wAn dIgO
1 min readJul 31, 2021

Taking photo and display it as a thumbnail

Steps -

1. Create a new project CameraSample.
2. Create the default activity CameraActivity and relative layout with an ImageView and an ImageButton

Relative Layout — This is the layout for the same.
activity_camera.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”
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=”.CameraActivity” >

<ImageButton
android:id=”@+id/buttonImage”
android:layout_width=”100dip”
android:layout_height=”100dip”
android:layout_below=”@+id/imageView1"
android:layout_centerHorizontal=”true”
android:layout_marginTop=”96dp”
android:cropToPadding=”true”
android:src=”@drawable/camera” />

<ImageView
android:id=”@+id/imageView1"
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”61dp”
android:src=”@drawable/frame” />

</RelativeLayout>

3. In the CameraActivity, set the layout as camera_activity.xml

CameraActivity.java

package com.vikshep;

import java.io.InputStream;

import android.os.Bundle;
import android.provider.MediaStore;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

@SuppressLint(“NewApi”)
public class CameraActivity extends Activity {
private static final int CAPTURE_IMAGE_CAPTURE_CODE = 0;
//Intent cameraIntent;
ImageButton takePhoto;
ImageView pic;
Bitmap bmp;

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

InputStream is = getResources().openRawResource(R.drawable.frame);
bmp = BitmapFactory.decodeStream(is);

takePhoto = (ImageButton) findViewById(R.id.buttonImage);
pic = (ImageView) findViewById(R.id.imageView1);

takePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, “Image Captured”, Toast.LENGTH_LONG).show();
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get(“data”);
pic.setImageBitmap(bmp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, “Cancelled”, Toast.LENGTH_LONG).show();
}
}
}

}

--

--