Get/Pick image from gallery in android programmatically

How to dynamically display image from android mobile phone gallery and Google photos & set into app on button click.

Getting image from android mobile phone device gallery is very important task to do because whenever we are trying to create chatting, matrimonial, social networking apps where app user needed to upload and share its own photos via application platform. So here is the complete step by step tutorial for Get/Pick image from gallery in android programmatically.

android-project-download-code-button

How to Get/Pick image from gallery in android programmatically.

First of all please add write external storage permission inside your AndroidManifest.xml file.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Code for MainActivity.java file.

 package com.pickiimagefromgallery_android_examples.com;

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.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 private static int IMG_RESULT = 1;
 String ImageDecode;
 ImageView imageViewLoad;
 Button LoadImage;
 Intent intent;
 String[] FILE;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 imageViewLoad = (ImageView) findViewById(R.id.imageView1);
 LoadImage = (Button)findViewById(R.id.button1);
 
 LoadImage.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 intent = new Intent(Intent.ACTION_PICK,
 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 
 startActivityForResult(intent, IMG_RESULT);
 
 }
 });
 
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 try {
 
 if (requestCode == IMG_RESULT && resultCode == RESULT_OK
 && null != data) {
 
 
 Uri URI = data.getData();
 String[] FILE = { MediaStore.Images.Media.DATA };
 
 
 Cursor cursor = getContentResolver().query(URI,
 FILE, null, null, null);
 
 cursor.moveToFirst();
 
 int columnIndex = cursor.getColumnIndex(FILE[0]);
 ImageDecode = cursor.getString(columnIndex);
 cursor.close();
 
 imageViewLoad.setImageBitmap(BitmapFactory
 .decodeFile(ImageDecode));
 
 }
 } catch (Exception e) {
 Toast.makeText(this, "Please try again", Toast.LENGTH_LONG)
 .show();
 }
 
 }
}

Code for activity_main.xml layout file.

<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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.pickiimagefromgallery_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:text="Click here to Get/Pick image from gallery in android programmatically" />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.pickiimagefromgallery_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 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>

Screenshots:

Click here to download Get/Pick image from gallery in android programmatically project.

16 Comments

  1. is it working for all types of android version please help me bro im facing image rote issue in some device .

  2. In KitKat and above, the Uri returned from Gallery is different and will crash the app once an image is selected.

  3. this coads dont work….

  4. please help me….

  5. help !!
    .
    .
    how to save captured image into gallery ?
    im using nexus 5 device

  6. Muchas gracias, me ayudo mucho tu codigo.
    ! Exitos !

  7. Hi Juned,
    Thanks for your tutorial,
    Code is not working when photo is not on device and it is on google photos cloud.
    In Moto or Pixel devices Gallery app is not there.
    So when you open to select it will open photos app, Choose the picture which is not in local device then it will not give the image.

Leave a Reply

Your email address will not be published. Required fields are marked *