title: android: Get real path from uri
body: <p>I want to sen file to server and I need to get real path from uri.</p>
  
  <p>My code:</p>
  
  <pre><code>public String getPathFromURI(Context context, Uri contentUri) {
      if ( contentUri.toString().indexOf("file:///") &gt; -1 ){
          return contentUri.getPath();
      }
  
      Cursor cursor = null;
      try { 
          String[] proj = { MediaStore.Images.Media.DATA };
          cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          cursor.moveToFirst();
          return cursor.getString(column_index);
      }finally {
            if (cursor != null) {
                cursor.close();
            }
      }
  }
  </code></pre>
  
  <p>And onActivityResult:</p>
  
  <pre><code>...
  imageName = data.getData();
  imagePath = getPathFromURI(getBaseContext(),imageName);
  
  Picasso.with(getBaseContext()).load(imageName).into(imageView);
  ...
  </code></pre>
  
  <p>How it's possible that image shows in ImageView , but imagePath is ALWAYS null ? :) Thanks</p>
  
  <p>EDIT:</p>
  
  <p>How i send image to server</p>
  
  <pre><code>HttpClient httpClient = new DefaultHttpClient();
  HttpContext localContext = new BasicHttpContext();
  HttpPost httpPost = new HttpPost([URL TO A SERVER]);
  
  MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
  entity.addPart("uploaded_file_1", new FileBody(new File( imagePath )));
  
  httpPost.setEntity(entity);
  httpClient.execute(httpPost, localContext);
  </code></pre>