title: Select photo from gallery android
body: <p>I'm trying to build an app that contains an ImageView .If I click it ,the gallery shows up so I can select a picture for the imageView.
  My Xml is so simple:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;ImageView
      android:layout_width="200dp"
      android:layout_height="200dp"
      android:id="@+id/imageView"
      android:layout_gravity="center_horizontal"
      android:clickable="true"/&gt;
  &lt;LinearLayout&gt;
  </code></pre>
  
  <p>and here's my main class</p>
  
  <pre><code>public class MainActivity extends Activity {
  ImageView img;
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      img=(ImageView)findViewById(R.id.imageView);
      setContentView(R.layout.cam);
      img.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent =new Intent();
              intent.setType("image/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent,"Select Contact Image"),1);
          }
      });
  
   }
   public void onActivityResult(int reqCode,int resCode, Intent data){
      if(resCode==RESULT_OK){
          if(reqCode==1){
              img.setImageURI(data.getData());
          }
      }
    }
  }
  </code></pre>