title: Getting Image from gallery in android
body: <p>I've been trying to get an image from gallery in this app and then on a button click I want to upload to my PHP server.</p>
  
  <p>The "selectedImagePath" comes as null why?
  If anyone has a solutions please help me! Thanks in advance.</p>
  
  <h2>onCreate()</h2>
  
  <pre><code>img = (ImageView) findViewById(R.id.ImageView01);
          Button browse = (Button) findViewById(R.id.Button01);
  
          browse.setOnClickListener(new View.OnClickListener() {
              @Override
              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);
              }
          });
  </code></pre>
  
  <h2>onActivityResult()</h2>
  
  <pre><code>public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode == RESULT_OK) {
              if (requestCode == SELECT_PICTURE) {
                  selectedImageUri = data.getData();
                  System.out.println(selectedImageUri);
                  selectedImagePath = getPath(selectedImageUri);//returns as null
                  System.out.println("Image Path : " + selectedImagePath);
                  img.setImageURI(selectedImageUri);
              }
          }
      }
  </code></pre>
  
  <h2>getPath()</h2>
  
  <pre><code>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>