camera-android

Android Camera example

Android Camera example

Today we are going to learn android camera. In some android application you need to integrate camera option for image capture.Here we also see the image selection from sd card of device also. For image capture we use code.

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

and image select from sd card we use the intent code
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

If you are using Android 4.4 (API level 19), it's no longer necessary for your app to request the android.permission.CAMERA permission otherwise you have to take permission.

Now let’s have a look to the source code

Activity class CameraActivity.java


 

package com.example.cameraandroidfromhome;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CameraActivity extends Activity implements OnClickListener {

    private Bitmap result_bitmap;
    private ImageView iv_show;
    private Button btn_camera;
    private Button btn_gallery;
public static int RESULT_MEDIA_IMAGE=444;
public static int RESULT_LOAD_IMAGE_CAMERA=555;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        
        
        
        iv_show=(ImageView)findViewById(R.id.iv_show);
        btn_camera=(Button)findViewById(R.id.btn_camera);
        btn_gallery=(Button)findViewById(R.id.btn_gallery);
        btn_camera.setOnClickListener(this);
        btn_gallery.setOnClickListener(this);
        
    
    }

    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

         super.onActivityResult(requestCode, resultCode, data);     

             if (requestCode == RESULT_MEDIA_IMAGE && resultCode == RESULT_OK && data!= null){
                // Recycle unused bitmaps
                if (result_bitmap != null) {
                    result_bitmap.recycle();
                }
                 Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    result_bitmap = (BitmapFactory.decodeFile(picturePath));
                    if(result_bitmap!=null){
                        
                           iv_show.setImageBitmap(result_bitmap);
                              
                        
                        
                   }
                   
                    }else if(requestCode == RESULT_LOAD_IMAGE_CAMERA && resultCode == RESULT_OK &&data!= null)
                    {
                        
                            // Recycle unused bitmaps
                            if (result_bitmap != null) {
                                result_bitmap.recycle();
                            }
                            
                            result_bitmap=(Bitmap)data.getExtras().get("data");
                            if(result_bitmap!=null){
                                
                                iv_show.setImageBitmap(result_bitmap);
                                
                           }
                             
                    }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_camera:
        
            Intent intent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(intent,  RESULT_LOAD_IMAGE_CAMERA);
            
        
            break;
            
        case R.id.btn_gallery:
             Intent in = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(in, RESULT_MEDIA_IMAGE);    
          
             break;

        default:
            break;
        }
        
    }

    }

 


and xml file activity_camera.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal" >

        <ImageView
            android:id="@+id/iv_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:text="Select Image" />

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

        <Button
            android:id="@+id/btn_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gallery" />

    </LinearLayout>

</LinearLayout>


Download Source Code

 

 

Related Post:-

Internet connection checking in android device

Custom Alert Dialog in android with custom xml layout

ListView in android example

Custom Toast example in android example

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget