Problem: How i pick image from native gallery and display it inside the ImageView?
Description:
First of all, to launch the native applications for Gallery, just write the below, there may be having many applications if you have installed other gallery like application, for example: QuickPic or any gallery application.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, SELECT_PICTURE);
Now we have to fetch the selected image inside the onActivityResult() method by overriding it inside our activity class.
GalleryImagePickActivity.java
package com.paresh.galleryimagepick; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; public class GalleryImagePickActivity extends Activity { /** Called when the activity is first created. */ private static final int SELECT_PICTURE = 1; ImageView imgBG; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgBG = (ImageView) findViewById(R.id.imgBG); } public void btnChooseBGClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, SELECT_PICTURE); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case SELECT_PICTURE: if(resultCode == RESULT_OK) { Uri uri = imageReturnedIntent.getData(); String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(projection[0]); String filePath = cursor.getString(columnIndex); cursor.close(); Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); Drawable d =new BitmapDrawable(yourSelectedImage); imgBG.setBackgroundDrawable(d); } } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <relativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp" > <button android:id="@+id/btnChooseBG" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="btnChooseBGClick" android:text="Choose Background" android:layout_centerHorizontal="true"> </button> <imageView android:id="@+id/imgBG" android:layout_width="400dp" android:layout_height="400dp" android:scaleType="fitXY" android:layout_below="@+id/btnChooseBG" android:layout_centerHorizontal="true"> </imageView> </relativeLayout>
Download Full source code from here: Android – Pick image from native gallery