Pages

Sunday, January 25, 2015

Capture ,Save and Preview Photos with Android



This tutorial demonstrates ,How to capture photos using an existing camera application.the main objectives are : Capture image then save and preview image 

 1.Open Android Studio /Eclipse and create a
new Android project.

 2.Go to our res/layout/activity_my.xml in Android Studio or res/layout/activity_main.xml in Eclipse and create our Main Activity Layout 


  •  The layout has One Button to call the camera activity
  •  And One ImageView to show captured image



<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=".MyActivity">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Capture"
        android:id="@+id/btnCapture"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_below="@+id/btnCapture"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:visibility="visible"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>


3.Then in our MyActivity.java file we :
  •  Define variables 
  •  Get reference to views 
  •  Add onclick listener to the button 
  •  Create intent to invokes an intent to capture a photo. with ACTION_IMAGE_CAPTURE action which is Standard Intent action that can be sent to have the camera application capture an image and return it. 
  • Call startActivityForResult to start the camera activity 
  • Save a File on External Storage : File(Environment.getExternalStorageDirectory(),"photo.jpg") 
  • We Override the onActivityResult() and import the image from the content provider Then set image to our imageview.



package com.example.morningsun.camera_app;

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

import java.io.File;

public class MyActivity extends ActionBarActivity {
    Button btnCap;
    Uri phUri;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        btnCap=(Button)findViewById(R.id.btnCapture);
        imageView=(ImageView)findViewById(R.id.imageView);

        btnCap.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){
                Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file =new File(Environment.getExternalStorageDirectory(),"photo.jpg");
                phUri=Uri.fromFile(file);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, phUri);
                startActivityForResult(intent, 0);

            }
        });
    }
    protected void onActivityResult(int Reqcode,int ResCode,Intent data){
        if(Reqcode==0){
            if(ResCode== RESULT_OK){
                getContentResolver().notifyChange(phUri,null);
                ContentResolver cr =getContentResolver();
                try{
                    Bitmap photo = MediaStore.Images.Media.getBitmap(cr,phUri);
                    imageView.setImageBitmap(photo);

                }catch (Exception ex){ex.printStackTrace();}


            }
        }
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

No comments:

Post a Comment