Thursday 1 August 2013

Image Pick From Gallery Android

Leave a Comment
Hello friends, lets begin with another useful exercise in android development. In this post I am going to display how to choose an image from the gallery. This post is useful to those who are making an application in which they need to access to the gallery and take out a photo from it.To demonstrate I have made a sample project named ImagePickFromGallery.

Step 1 :- activity_image_pick.xml.
In xml I am having a button to choose from gallery and one ImageView
<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"
  >
    <Button
        android:id="@+id/btn_image_pick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="120dp"
        android:text="ImagePick" />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_image_pick"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="33dp"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

Step 2:- ImagePick.java
package com.imagepickfromgallery;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class ImagePick extends Activity {
   
      private static final int REQUEST_CODE = 1;
      private Bitmap bitmap;
      private ImageView imageView;
      private Button btnImagePick;
      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_pick);
       
        btnImagePick = (Button) findViewById(R.id.btn_image_pick);
        imageView = (ImageView) findViewById(R.id.imageView1);
       
        btnImagePick.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    startActivityForResult(intent, REQUEST_CODE);
           
               
            }
        });
    }
     
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
          try {
            // We need to recyle unused bitmaps
            if (bitmap != null) {
              bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            imageView.setImageBitmap(bitmap);
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
        super.onActivityResult(requestCode, resultCode, data);
      }
}


                                                                                        Android For Beginners

0 comments:

Post a Comment