1private void pickFromGallery(){
2 //Create an Intent with action as ACTION_PICK
3 Intent intent=new Intent(Intent.ACTION_PICK);
4 // Sets the type as image/*. This ensures only components of type image are selected
5 intent.setType("image/*");
6 //We pass an extra array with the accepted mime types. This will ensure only components with these MIME types as targeted.
7 String[] mimeTypes = {"image/jpeg", "image/png"};
8 intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes);
9 // Launching the Intent
10 startActivityForResult(intent,GALLERY_REQUEST_CODE);
11 }
12
13 public void onActivityResult(int requestCode,int resultCode,Intent data){
14 // Result code is RESULT_OK only if the user selects an Image
15 if (resultCode == Activity.RESULT_OK)
16 switch (requestCode){
17 case GALLERY_REQUEST_CODE:
18 //data.getData returns the content URI for the selected Image
19 Uri selectedImage = data.getData();
20 imageView.setImageURI(selectedImage);
21 break;
22 }
23 }