Monday, July 1, 2013

Selecting image from gallery and capturing from camera and setting it to ImageView

Basic image application in android requires selecting an image from gallery or capturing from camera.

Basically your any android app will require a XML file and associated Java class.
XML file here will contain an imageview, and two buttons. one to capture from camera and one to select from gallery.

1. Capturing an image and selecting from gallery are handled using switch case.

Source code for Java class :

package com.example.photoshop;

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;


public class ImageSelector extends Activity implements OnClickListener {
   
    protected static final int SELECT_PICTURE = 1;
    static final int camera=0;
    Bitmap bmp;
    Intent i;
    ImageView image;
    Button galleryy,cameraa;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageselector);
   
        galleryy = (Button)findViewById(R.id.gallery);
        cameraa = (Button)findViewById(R.id.camera);

        image = (ImageView)findViewById(R.id.image);
   
        galleryy.setOnClickListener(this);
        cameraa.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId())
        {

        case R.id.gallery: 
             i= new Intent(Intent.ACTION_GET_CONTENT);
            i.setType("image/*");                   
            startActivityForResult(Intent.createChooser(i,"Select Picture"), SELECT_PICTURE);
            break; 


        case R.id.camera :
            i= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
             startActivityForResult(i, camera);
             break;
        }   
    }

   
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK  )
        {
            if(requestCode == camera)
            {
            Bundle bdata = data.getExtras();
            bmp = (Bitmap)bdata.get("data"); 
 

               image.setImageBitmap(bmp);
            }
         
            else 
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
                String imageLocation = null;
                if(cursor!=null)
                {
                    int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    imageLocation = cursor.getString(column_index);
                    bmp = BitmapFactory.decodeFile(imageLocation);  
                   image.setImageBitmap(bmp);
                }
             
            }
         
        }
    }
}

 

2. XML will be same in either case. 

Source code for xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/subtle_grunge">
   
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/cd"
        android:src="@android:drawable/screen_background_dark" />

    
    <Button
        android:id="@+id/gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="81dp"
        android:text="@string/gallery" />

    <Button
        android:id="@+id/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gallery"
        android:layout_below="@+id/gallery"
        android:layout_marginTop="80dp"
        android:text="@string/camera" />

</RelativeLayout>


No comments:

Post a Comment