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 .

SOLUTION:
Step 1)  Create/ initiate an image view as

               ImageView yourimgView=(ImageView)findViewById(R.id.imageview01);

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


Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
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);
           cursor.moveToFirst();

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




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

If you have any problems ,please send feedback.

10 comments:

  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,
    Satish.

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

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

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

    ReplyDelete
  5. where i put this code ??

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

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

    ReplyDelete
  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.

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

    Thanks

    ReplyDelete

Android Developers Blog

Ram's shared items