title: Changing an ImageView in fragment on clicking the ImageView
body: <p>I am using <code>Fragments</code> in my app. I have an <code>ImageView</code> in a <code>Fragment</code>. on clicking the <code>ImageView</code> user should be able to select the image from the gallery and the image should be displayed in the same <code>ImageView</code>. I tried this separately in an <code>Activity</code> Class and it is working fine but how can I do this in a <code>Fragment</code> Class? The code I tried is-</p>
  
  <pre><code>  public class FragmentCreateGroup extends Fragment {
      ImageView group;
      EditText et;
      LayoutInflater glob_inflater;
      View glob_view;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
  
  
  
      }
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
              View v = inflater.inflate(R.layout.fragment_creategroup, container, false);
              glob_view=v;
              glob_inflater=inflater;
              et = (EditText) v.findViewById(R.id.cr_group_grpname_input);
              group = (ImageView) v.findViewById(R.id.group_image);
              group.setOnClickListener(new View.OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      Intent intent = new Intent();
                       intent.setType("image/*");
                       intent.setAction(Intent.ACTION_GET_CONTENT);
                       startActivityForResult(Intent.createChooser(intent,
                               "Select Picture"), 1);
                  }
  
  
              });
              return v;
      }
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode == -1) {
              if (requestCode == 1) {
                  Uri selectedImageUri = data.getData();
                  String selectedImagePath = getPath(selectedImageUri);
  
                  BitmapFactory.Options options = new BitmapFactory.Options();
  
                  options.inSampleSize = 2;
                  Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, options);
  
                  group.setImageBitmap(BitmapFactory.decodeFile(selectedImagePath));
              }
          }
      }
  
       public String getPath(Uri uri) {
           // just some safety built in 
           if( uri == null ) {
               // TODO perform some logging or show user feedback
               return null;
           }
           Activity act=new Activity();
           // try to retrieve the image from the media store first
           // this will only work for images selected from gallery
           String[] projection = { MediaStore.Images.Media.DATA };
           Cursor cursor =act.managedQuery(uri, projection, null, null, null);
           if( cursor != null ){
               int column_index = cursor
               .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
               cursor.moveToFirst();
               return cursor.getString(column_index);
           }
           // this is our fallback here
           return uri.getPath();
   }
      public void onStart() {
          // TODO Auto-generated method stub
          super.onStart();
  
      }
  
  }
  </code></pre>