Tuesday, July 7, 2015

Android Application for Taking Photos

Our activity_main.xml file will be as follows

<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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

     <TextView         android:id="@+id/textview1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="Press the button,Capture photo and enjoy"/>

    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Take Photo"        android:layout_below="@+id/textview1"/>
    <ImageView        android:id="@+id/imageview1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:layout_below="@+id/button1"        />
</RelativeLayout>


The MainActivity.java file code is written as below..

import android.app.Activity;
import android.content.Intent;
import  android.graphics.Bitmap;
import  android.provider.MediaStore;
import  android.support.v7.app.ActionBarActivity;
import  android.os.Bundle;
import  android.view.Menu;
import  android.view.MenuItem;
import  android.view.View;
import  android.widget.Button;
import  android.widget.ImageView;


public class MainActivity extends Activity {

    Button btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUEST=1313;

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

        btnTakePhoto=(Button) findViewById(R.id.button1);
        imgTakenPhoto=(ImageView) findViewById(R.id.imageview1);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {
        public void onClick(View v)
        {
            Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent,CAM_REQUEST);


        }
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        super.onActivityResult(requestCode,resultCode,data);

        if(requestCode==CAM_REQUEST)
        {
            Bitmap thumbnail=(Bitmap) data.getExtras().get("data");
            imgTakenPhoto.setImageBitmap(thumbnail);
        }
    }


}

No comments:

Post a Comment