title: Create File from Uri type android
body: <p>I'm trying to select image from gallery then convert this image to File and send it via HttpPost but I'm getting always <code>FileNotFoundException</code>. This my Code : </p>
  
  <p><strong>Selecting the photo</strong></p>
  
  <pre><code>public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode == Activity.RESULT_OK) {
              if (requestCode == 1) {
                  // currImageURI is the global variable I’m using to hold the content:
                  currImageURI = data.getData();
                  //Save the currImageUri (URI type) to global variable.
                  photosHolder.getInstance().setOneIm(currImageURI);
              }
          }
      }
  </code></pre>
  
  <p><strong>Converting the photo</strong></p>
  
  <pre><code>                // First Try
                  File myFile = new File((photosHolder.getInstance().getOneIm()).toString());
                  params.put("visit_report[photos_attributes][0][file]",myFile); **The exception Raised here
  // second try 
                  File myFile2 = new File((photosHolder.getInstance().getOneIm()).getPath());
                  params.put("visit_report[photos_attributes][1][file]",myFile2); ** Also here
  </code></pre>
  
  <p>And those are myFiles Value while debuging : </p>
  
  <pre><code>mFile : content:/com.android.providers.media.documents/document/image%3A19143
  myFile2 : /document/image:19143
  </code></pre>
  
  <p>So any help ?</p>
  
  <hr>
  
  <p><strong>Update</strong> 
  I have tried this solution also :</p>
  
  <pre><code>//get The real path from uri then save it (and then use it to create the file)
  photosHolder.getInstance().setUriString(getRealPathFromURI(currImageURI));
  
  //Convert the image URI to the direct file system path of the image file
      private String getRealPathFromURI(Uri contentURI) {
          String result = "";
          try {
          Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);
          if (cursor == null) { // Source is Dropbox or other similar local file path
              result = contentURI.getPath();
          } else {
              cursor.moveToFirst();
              int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
              result = cursor.getString(idx); // Exception raised HERE
              cursor.close(); }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return result;
      }
  </code></pre>
  
  <p>But I got <code>java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it</code> and the <code>idx</code> var == to <code>-1</code></p>
  
  <p>Also I have tried @Praneeth Kalluri Solution but it returns always null as result.</p>