title: Get filepath and filename of selected gallery image in Android
body: <p>I am creating an app which uploads a selected image from the gallery and uploads it to a web service.  The webservice requires the filename of selected image plus a base64 encoding of the file contents.  I have managed to achieve this with a hardcoded file path.  However, I am struggling to get the real filepath of the image.  I have read around the web and have this code, but it does not work for me:</p>
  
  <pre><code> public void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (resultCode == RESULT_OK) {
       Uri selectedImageUri = data.getData();
       String[] projection = {MediaStore.Images.Media.DATA};
  
       try {
         Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
         cursor.moveToFirst();
  
         int columnIndex = cursor.getColumnIndex(projection[0]);
         String picturePath = cursor.getString(columnIndex);
         cursor.close();
         Log.d("Picture Path", picturePath);
       }
       catch(Exception e) {
         Log.e("Path Error", e.toString());
       }
     }
   }
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>java.lang.NullPointerException
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Forgot to mention I am using Kitkat.  It looks like my problem is KitKat related.  I found this (see below) which helped me get my app working:</p>
  
  <p><a href="https://stackoverflow.com/questions/19834842/android-gallery-on-kitkat-returns-different-uri-for-intent-action-get-content">Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT</a></p>