Monday, August 23, 2010

Pick one Image from Gallery and use it in your ANDROID application

AIM: Suppose you need to open up gallery and pick one Image from Gallery and use it in your   ANDROID application .

Step 1)  Create/ initiate an image view as

               ImageView yourimgView=(ImageView)findViewById(;

Step 2)  open gallery from your application by using the following lines.

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 0);

Step 3) Get the selected image data and put that image in  a image view from gallery to your application by using following lines.

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
   super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

   switch(requestCode) {
   case 0:
       if(resultCode == RESULT_OK){
           Uri selectedImage = imageReturnedIntent.getData();
           String[] filePathColumn = {MediaStore.Images.Media.DATA};

           Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

           int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
           String filePath = cursor.getString(columnIndex); // file path of selected image
                   //  Convert file path into bitmap image using below line.
           Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                   // put  bitmapimage in your imageview

Result : You can see the selected image from gallery in your imageview .

If you have any problems ,please send feedback.


  1. Hi this is satish ,Your articles are awesome ,Really its wonderfull,one more request from me ,that is please tell me how to open music files from our application and how to send those files to selected users.please provide me a sample code for above application.

    Thanks and regards,
    Awaiting for your response,

  2. Great!!! It helped me. Thanks for the post.

  3. Great blog man !! could you make a video example instead of image ? Thanks and regards

  4. Love the blog, very handy when I just want to look for quick answers. Keep up the good work!

  5. where i put this code ??

  6. Will this code going to run for emulator 4.0.3 in android...?

  7. Hello! How can I create the photo album. When I choose path for selecting image, there is no image in my album

  8. Hi this post is really helpful. I want to pick image which i have done successful using above code. i don't wanna show image using imageview i want to convert imageView image into byte code.

  9. If i had two imageviews in my activity how do i open the gallery to pick the second image?



Android Developers Blog

Ram's shared items