Monday, May 16, 2011

Pick Image from Gallery set to Image View

package com.pickimagefromgal.activities;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Home extends Activity
{
    Button btnGal;
    ImageView ivGalImg;
    Bitmap bmp;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btnGal         =     (Button)findViewById(R.id.btnGallary);
        ivGalImg     =     (ImageView)findViewById(R.id.ivImage);
       
        btnGal.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View arg0)
            {
                openGallery();               
            }
        });
    }
   
    private void openGallery()
    {
        Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, 1);
    }
   
  @Override
  protected void onActivityResult(int requestCode, int resultcode, Intent intent)
  {
      super.onActivityResult(requestCode, resultcode, intent);
     
      if (requestCode == 1)
      {
          if (intent != null && resultcode == RESULT_OK)
          {             
             
                Uri selectedImage = intent.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 filePath = cursor.getString(columnIndex);
                cursor.close();
             
                if(bmp != null && !bmp.isRecycled())
                {
                    bmp = null;               
                }
                               
                bmp = BitmapFactory.decodeFile(filePath);
                ivGalImg.setBackgroundResource(0);
                ivGalImg.setImageBitmap(bmp);             
          }
          else
          {
              Log.d("Status:", "Photopicker canceled");           
          }
      }
  }
}

main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/ivImage"
        android:background="@drawable/icon"   
        >
    </ImageView>
   
    <Button
        android:layout_width="wrap_content"
        android:text="Click To Open Gallary"
        android:id="@+id/btnGallary"
        android:layout_height="wrap_content">
    </Button>
   
</LinearLayout>

Manifast: 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.pickimagefromgal.activities"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Home"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
   
   
</manifest>

Out Put: