Thursday, 18 September 2014

Loading Image from Gallery example in Android

First of all create some string values in strings.xml as follows
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Loading Images From Gallery</string>
    <string name="hello_world">Hello world!</string>
    <string name="load_image_button_text">Load Image From Gallery</string>
    <string name="image">image</string>


</resources>


Next create one button and image as follows
activity_main.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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="loadImageFromGallery"
        android:text="@string/load_image_button_text" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/image"
        android:src="@drawable/ic_launcher" />

</LinearLayout>


MainActivity.java:
package com.ram.loadingimagesfromgallery;

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

public class MainActivity extends Activity
{
    // ImgeView to display image from gallery
    ImageView imageView;
    // variable to hold request code value
    private final int REQ_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initializing user interface from activity_main.xml file
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    // Method to listen action for the button
    public void loadImageFromGallery(View view)
    {
        // Intent to mention which kind of action to be performed
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        // To launch intent
        startActivityForResult(intent, REQ_CODE);
    }

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

        if (requestCode == REQ_CODE && resultCode == RESULT_OK && data != null)
        {
            // Retrieve data this intent is operating on
            Uri selectedImage = data.getData();
            // The Media provider contains meta data for all available media on
            // both internal and external storage devices.

            String[] filePathColumn =
            { MediaStore.Images.Media.DATA };
            // Query the given URI, returning a Cursor over the result set.
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn,
                    null, null, null);
            // Move the cursor to the first row.
            cursor.moveToFirst();
            // To get given column index number
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            // Returns the value of the requested column as a String.
            String picturePath = cursor.getString(columnIndex);
            // Closes the Cursor, releasing all of its resources and making it
            // completely invalid. Unlike deactivate() a call to requery() will
            // not make the Cursor valid again.

            cursor.close();
            // Sets a Bitmap as the content of this ImageView.
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }
}