title: Android and FileProvider and getting patth from URI
body: <p>I have some problems which are puzzling to me. I seem unable to get the correct <strong>real</strong> path for a file based Uri. This is the code with end result variable values shown below it:</p>
  
  <pre><code>  String shareFile_Path = "";
    String contentURI_Path_Real = "";          
    String contentURI_Path_Simple = "";          
    Uri myContentUri_Camera = null;          
  
    Bitmap myBitmap = SharedCode.sharedGetImageAsBitmapFromResource(thisActivityContext, R.drawable.replace__logo__app_256_256);           
    File mySharedFile_Camera = SharedCode.sharedSaveBitmapToInternalStoragePath(thisActivityContext, "shared", "camera.jpg", myBitmap);
  
    if ( (mySharedFile_Camera != null) &amp;&amp; (mySharedFile_Camera.exists()) ) {
  
      shareFile_Path = mySharedFile_Camera.getAbsolutePath();
  
      try {
        myContentUri_Camera = FileProvider.getUriForFile(thisActivityContext, "com.example.app.fileprovider", mySharedFile_Camera);
      }
      catch (Exception e) {
        String debug_s = e.toString();
      }            
      if (myContentUri_Camera != null) {
        contentURI_Path_Simple = myContentUri_Camera.getPath(); 
        contentURI_Path_Real = SharedCode.sharedGetRealPathFromURI(thisActivityContext, myContentUri_Camera);
      }  
    }  
  </code></pre>
  
  <p>End results of different variables:</p>
  
  <ul>
  <li><strong>sharedFile_Path</strong> = <em>"/data/data/com.example.app/files/shared/camera.jpg"</em></li>
  <li><strong>contentURI_Path_Simple</strong> = <em>"/providershared/camera.jpg"</em></li>
  <li><strong>contentURI_Path_Real</strong> = <em>""</em></li>
  </ul>
  
  <p>As can be seen, the problem is the value for <strong>contentURI_Path_Real</strong> which is why the complete code for <strong>SharedCode.sharedGetRealPathFromURI</strong> is here - something I original found through SO:</p>
  
  <pre><code>  public static String sharedGetRealPathFromURI(Context context, Uri contentUri) {
      String res = "";
      if (contentUri != null) {      
        Cursor cursor = null;      
        try {
          cursor = context.getContentResolver().query(contentUri, null, null, null, null);
          if (cursor !== null) { 
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            if (idx &gt; -1) {
              res = cursor.getString(idx);
            }  
          }  
        } 
        catch(Exception e){
        }         
        if (cursor != null) {
          cursor.close();
        }      
      }              
      return res;             
    }
  </code></pre>
  
  <p>When stepping through the code, it appears this <strong>if (idx > -1)</strong> yields <strong>false</strong>.</p>
  
  <p>...</p>
  
  <p>This is the relevant code for <strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="com.example.app.fileprovider"
  android:grantUriPermissions="true"
  android:exported="false"&gt;
  &lt;meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_filepaths" /&gt;
  &lt;/provider&gt;
  </code></pre>
  
  <p>This is the complete code for <strong>provider_filepaths.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;paths&gt;
      &lt;files-path path="shared/" name="providershared" /&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>...</p>
  
  <p>In the end, I need a valid <strong>myContentUri_Camera</strong> for when calling a camera intent:</p>
  
  <pre><code>  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if ( (myContentUri_Camera != null) &amp;&amp; (contentURI_Path_Real != "") ) {
      cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, myContentUri_Camera);
    }  
    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
  </code></pre>
  
  <p>As it is now, if <strong>myContentUri_Camera</strong> is passed, the camera crashes the app when done - before <strong>onActivityResult</strong> returns. I suspect that is because it has no "real path" which is why I am asking about the path issue here.</p>