title: How do I open a bitmap from a URI that is the result of taking a camera picture?
body: <p>I'm pretty bewildered here and have tried numerous suggestions I've seen on SO. I was following the Xamarin recipe for accessing the camera on Android and taking a picture. There were some things missing/out of date, but regardless I got to the point where I need to call <code>BitmapFactory.DecodeFile</code> so I can open the image the user just took and resize it to display in my view. The problem I ran into is that I can't use file paths, I have to use URIs. So I followed the instructions <a href="https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed">in this SO thread</a></p>
  
  <p>and set up my file provider stuff. I have this file in my resources/xml directory: provider_paths.xml (MyApp being renamed from my actual app details)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;external-path name="MyApp" path="."/&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>Then in the manifest I added this inside the  section:</p>
  
  <pre><code>&lt;provider android:name="android.support.v4.content.FileProvider" android:authorities="com.myapp.provider" android:exported="false" android:grantUriPermissions="true"&gt;
          &lt;meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /&gt;
  &lt;/provider&gt;
  </code></pre>
  
  <p>I ensure the directory exists by doing:</p>
  
  <pre><code>var file = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "MyApp");
  Directory = file.AbsolutePath;
  if (!file.Exists())
      file.Mkdirs();
  </code></pre>
  
  <p>(Directory being a string variable).</p>
  
  <p>I create the intent and file name like so:</p>
  
  <pre><code>Intent intent = new Intent(MediaStore.ActionImageCapture);              
  var file = new File(Directory, $"myPhoto_{Guid.NewGuid()}.jpg");
  BaseFilename = file.Name;
  
  _uri = FileProvider.GetUriForFile(MainActivity.Instance, "com.myapp.provider", file);
  
  intent.PutExtra(MediaStore.ExtraOutput, _uri);
  intent.AddFlags(ActivityFlags.GrantReadUriPermission);
  </code></pre>
  
  <p>(BaseFilename being a string variable in this class)</p>
  
  <p>At first I thought maybe I should somehow convert the URI to a file path so that I can call BitmapFactory.DecodeFile. I followed several SO threads and none of them worked. I got various exceptions when they all started doing ContentResolver.Query or cursor operations. </p>
  
  <p>So I figured, well, there's a BitmapFactory.DecodeStream method, why not use that instead? I tried getting a stream from the URI like so:</p>
  
  <pre><code>var stream = MainActivity.Instance.ContentResolver.OpenInputStream(_uri)
  </code></pre>
  
  <p>But when I do this I get an exception: <code>Java.Lang.Exception: open failed: ENOENT (No such file or directory)</code></p>
  
  <p>An example of what the URI looks like when I try this:</p>
  
  <pre><code>content://com.myapp.provider/MyApp/Pictures/MyApp/myPhoto_2d13056f-6d23-461a-ad4b-532e10d9cb75.jpg
  </code></pre>
  
  <p>I'm so confused at this point about what's even the problem, and having a hard time with a lot of outdated Xamarin samples that I don't know where to look or what even is the real underlying problem here. Any insight would be greatly appreciated. BTW in case it matters, I at least go the permission stuff right. I already prompted for camera and storage access (I already dealt with errors regarding not having permission for camera/read/write external storage so at least that's over with).</p>