Tuesday, 28 February 2017

Select Image From Gallery Android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_weight="1"
    android:orientation="vertical"
    tools:context="com.androidsimplifycodes.example.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:gravity="center">

        <Button
            android:id="@+id/btnGallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gallery"
            android:textColor="@android:color/black"
            android:textStyle="bold" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:gravity="center"
        android:padding="15dp">

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@null"
            android:scaleType="fitXY"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/boarder" />

    </RelativeLayout>

</LinearLayout>
boarder.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="1.5dip"
        android:color="#000000" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>
MainActivity.java

package com.androidsimplifycodes.example;

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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    protected Button btnGallery;
    protected ImageView ivImage;

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

        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // start activity for result with request code
                startActivityForResult(i, 17);
            }
        });
    }

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

        if (resultCode == Activity.RESULT_OK && requestCode == 17) {
            // get the data from the getData() method which is return the Uri of the selected image
            Uri uri = data.getData();
            String[] filePath = {MediaStore.Images.Media.DATA};
            // fetch the selected image path using content resolver
            // which is return the object of the cursor
            Cursor cursor = getContentResolver().query(uri, filePath, null, null, null);
            // this one mandatory for the developer to check cursor is null or not
            if (cursor != null) {
                // set the cursor at the first position or move cursor pointer at the beginning
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePath[0]);
                String picturePath = cursor.getString(columnIndex);
                // this one also mandatory for the developer to close the open cursor
                cursor.close();

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                // required bitmap into ARGB_8888 format
                Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
                ivImage.setImageBitmap(bitmap);
            }
        }
    }

    private void initView() {
        btnGallery = (Button) findViewById(R.id.btnGallery);
        ivImage = (ImageView) findViewById(R.id.ivImage);
    }
}
Output :