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.
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.
ReplyDeleteThanks and regards,
Awaiting for your response,
Satish.
Great!!! It helped me. Thanks for the post.
ReplyDeleteGreat blog man !! could you make a video example instead of image ? Thanks and regards
ReplyDeleteLove the blog, very handy when I just want to look for quick answers. Keep up the good work!
ReplyDeletewhere i put this code ??
ReplyDeleteWill this code going to run for emulator 4.0.3 in android...?
ReplyDeleteHello! How can I create the photo album. When I choose path for selecting image, there is no image in my album
ReplyDeleteHi 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.
ReplyDeletethanks!
ReplyDeleteIf i had two imageviews in my activity how do i open the gallery to pick the second image?
ReplyDeleteThanks