title: Android: How to display selected image gallery in ImageView?
body: <p>I follow instruction from this <a href="http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/" rel="nofollow">http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/</a>.</p>
  
  <p>But I'm having this problem...</p>
  
  <p>The selected image from gallery is not displaying in my imageview.</p>
  
  <p>this is my code for layout:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  &lt;TextView
      android:id="@+id/lbl_name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="28dp"
      android:layout_marginTop="150dp"
      android:text="Name"
      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  &lt;EditText
      android:id="@+id/txt_name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/lbl_name"
      android:layout_below="@+id/lbl_name"
      android:ems="10"
      android:inputType="textPersonName" /&gt;
  
  &lt;TextView
      android:id="@+id/lbl_description"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/txt_name"
      android:layout_below="@+id/txt_name"
      android:text="Description"
      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  &lt;EditText
      android:id="@+id/txt_description"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/lbl_description"
      android:layout_below="@+id/lbl_description"
      android:ems="10"
      android:inputType="textMultiLine"
      android:minHeight="100dip"
      android:maxHeight="100dip"
      android:scrollbars="horizontal" &gt;
  
      &lt;requestFocus /&gt;
  &lt;/EditText&gt;
  
  &lt;TextView
      android:id="@+id/lbl_clue"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/txt_description"
      android:layout_below="@+id/txt_description"
      android:text="Clue"
      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  &lt;EditText
      android:id="@+id/txt_clue"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/lbl_clue"
      android:layout_below="@+id/lbl_clue"
      android:ems="10"
      android:inputType="textMultiLine"
      android:minHeight="50dip"
      android:maxHeight="50dip"
      android:scrollbars="horizontal" /&gt;
  
  &lt;Button
      android:id="@+id/btn_submit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/txt_clue"
      android:layout_below="@+id/txt_clue"
      android:text="Save" /&gt;
  
  &lt;Button
      android:id="@+id/btn_clear"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/txt_clue"
      android:layout_toLeftOf="@+id/btn_submit"
      android:text="Clear" /&gt;
  
  &lt;ImageView
      android:id="@+id/imageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/txt_name"
      android:layout_alignLeft="@+id/lbl_name"
      android:maxHeight="80dip"
      android:maxWidth="80dip"
      android:src="@drawable/icon_game" /&gt;
  
  &lt;Button
      android:id="@+id/btn_select"
      style="?android:attr/buttonStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/imageView1"
      android:layout_marginBottom="47dp"
      android:layout_marginLeft="22dp"
      android:layout_toRightOf="@+id/imageView1"
      android:text="Select Image" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>code of the class:</p>
  
  <pre><code>@SuppressLint("NewApi")
  public class NewDataActivity extends Activity {
  
  
  
      final TestAdapter mDbHelper = new TestAdapter(this);  
      private static final int SELECT_PICTURE = 1;
  
      private String selectedImagePath;
      private ImageView img;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_new_data);
          getActionBar().setDisplayHomeAsUpEnabled(true);
  
          final Button btn_submit = (Button) findViewById(R.id.btn_submit);
          final Button btn_select = (Button) findViewById(R.id.btn_select);
          final EditText txt_name = (EditText) findViewById(R.id.txt_name);
          final EditText txt_desc = (EditText) findViewById(R.id.txt_description);
          final EditText txt_clue = (EditText) findViewById(R.id.txt_clue);
  
          img = (ImageView)findViewById(R.id.imageView1);
  
          mDbHelper.createDatabase();       
          mDbHelper.open();
  
          btn_submit.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
  
                  if(isEmpty(txt_name) || isEmpty(txt_desc)){
                      if(isEmpty(txt_name)){
                          Toast.makeText(getApplicationContext(), "Name must not empty. ", Toast.LENGTH_SHORT).show();
                      }
                      else if(isEmpty(txt_desc)){
                          Toast.makeText(getApplicationContext(), "Description must not empty. ", Toast.LENGTH_SHORT).show();
                      }
                  } else { 
                      mDbHelper.insertData(txt_name.getText().toString(), txt_desc.getText().toString(), txt_clue.getText().toString());
                      mDbHelper.close();
  
                      Intent myIntent = new Intent(v.getContext(), ManageCustom.class);
                      startActivityForResult(myIntent, 0);
  
                  }
              }   
          });
  
          btn_select.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
  
                  Intent intent = new Intent();
                  intent.setType("image/*");
                  intent.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
              }   
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_new_data, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          // Respond to the action bar's Up/Home button
              case android.R.id.home:
                  NavUtils.navigateUpFromSameTask(this);
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      private boolean isEmpty(EditText etText) {
          return etText.getText().toString().trim().length() == 0;
      }
  
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode == RESULT_OK) {
              if (requestCode == SELECT_PICTURE) {
                  Uri selectedImageUri = data.getData();
                  selectedImagePath = getPath(selectedImageUri);
                  System.out.println("Image Path : " + selectedImagePath);
                  img.setImageURI(selectedImageUri);
              }
          }
      }
  
      public String getPath(Uri uri) {
          String[] projection = { MediaStore.Images.Media.DATA };
          Cursor cursor = managedQuery(uri, projection, null, null, null);
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          cursor.moveToFirst();
          return cursor.getString(column_index);
      }
  }
  </code></pre>
  
  <p>I don't know what is wrong with my code.</p>
  
  <p>Can anyone help me?</p>