title: Permissions problem when selecting image from gallery on Android
body: <p>I'm starting with Xamarin to be able to create an app that runs both on iOS and Android.
  The first thing I wanted to do is to click a button -> Open Gallery -> Select image -> Display selected image on a Image label</p>
  
  <p>The current content of MainPage.xaml is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:HelloWorld" x:Class="HelloWorld.MainPage"&gt;
      &lt;StackLayout&gt;
          &lt;!-- Place new controls here --&gt;
          &lt;Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /&gt;
          &lt;Label Text="0" x:Name="lbl_counter" HorizontalOptions="Center" VerticalOptions="StartAndExpand" FontSize="Large"/&gt; 
          &lt;Image x:Name="img_selected" Source="" /&gt; 
          &lt;Button Text="Click Me" Clicked="Handle_Clicked" VerticalOptions="StartAndExpand"/&gt;
      &lt;/StackLayout&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p>I'm using Plugin.Media so, when the button gets clicked I run this code:</p>
  
  <pre><code>async void Handle_Clicked(object sender, System.EventArgs e){
      lbl_counter.Text = (++counter).ToString();
  
      await CrossMedia.Current.Initialize();
      if (!CrossMedia.Current.IsPickPhotoSupported){
          await DisplayAlert("no upload", "picking a photo is not supported", "ok");
          return;
      }
  
      var file = await CrossMedia.Current.PickPhotoAsync();
      if (file == null)
          return;
  
      img_selected.Source = ImageSource.FromStream(file.GetStream);
  }
  </code></pre>
  
  <p>After a bit (a lot) of effort I managed to get it working on iOS.
  But I can't get it working on Android. It keeps complaining about permissions.
  I already followed all I could think of from:</p>
  
  <ul>
  <li><a href="https://forums.xamarin.com/discussion/89455/how-to-loading-images-from-gallery" rel="nofollow noreferrer">https://forums.xamarin.com/discussion/89455/how-to-loading-images-from-gallery</a></li>
  <li><a href="https://github.com/jamesmontemagno/MediaPlugin" rel="nofollow noreferrer">https://github.com/jamesmontemagno/MediaPlugin</a></li>
  <li><a href="https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/" rel="nofollow noreferrer">https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/</a></li>
  </ul>
  
  <p>Any idea? </p>
  
  <hr>
  
  <p>Adding information from the comments</p>
  
  <p>Comment 1:</p>
  
  <p>I don't get a (visible) stack trace in Xamarin.
  I get this if I copy from the error window:</p>
  
  <pre><code>Plugin.Media.Abstractions.MediaPermissionException: Storage permission(s) are required.
  </code></pre>
  
  <p>In the Output Window I can see:</p>
  
  <pre><code>Does not have storage permission granted, requesting.
  Storage permission Denied.
  </code></pre>
  
  <hr>
  
  <p><strong>CODE</strong></p>
  
  <p><em>MainPage.xaml.cs</em></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Xamarin.Forms;
  using Plugin.Media;
  using Plugin.Permissions;
  using Plugin.Permissions.Abstractions;
  
  namespace HelloWorld
  {
      public partial class MainPage : ContentPage
      {
          private int counter;
          public MainPage()
          {
              InitializeComponent();
          }
  
          async void Handle_Clicked(object sender, System.EventArgs e)
          {
              lbl_counter.Text = (++counter).ToString();
  
              /******************************/
              try
              {
                  var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
                  if (status != PermissionStatus.Granted)
                  {
                      if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
                      {
                          await DisplayAlert("Need Storage", "Gunna need that Storage", "OK");
                      }
  
                      var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
                      status = results[Permission.Storage];
                  }
  
                  if (status == PermissionStatus.Granted)
                  {
                      await CrossMedia.Current.Initialize();
                      if (!CrossMedia.Current.IsPickPhotoSupported)
                      {
                          await DisplayAlert("no upload", "picking a photo is not supported", "ok");
                          return;
                      }
  
                      var file = await CrossMedia.Current.PickPhotoAsync();
                      if (file == null)
                          return;
  
                      img_selected.Source = ImageSource.FromStream(file.GetStream);
                  }
                      else if (status != PermissionStatus.Unknown)
                  {
                      await DisplayAlert("Storage Permission Denied", "Can not continue, try again.", "OK");
                  }
              }
              catch
              {
                  //...
              }
              /******************************/
  
          }
      }
  }
  </code></pre>
  
  <p><em>MainPage.xaml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:HelloWorld" x:Class="HelloWorld.MainPage"&gt;
      &lt;StackLayout&gt;
          &lt;!-- Place new controls here --&gt;
          &lt;Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /&gt;
          &lt;Label Text="0" x:Name="lbl_counter" HorizontalOptions="Center" VerticalOptions="StartAndExpand" FontSize="Large"/&gt; 
          &lt;Image x:Name="img_selected" Source="" /&gt; 
          &lt;Button Text="Click Me" Clicked="Handle_Clicked" VerticalOptions="StartAndExpand"/&gt;
      &lt;/StackLayout&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p><em>MainActivity.cs</em></p>
  
  <pre><code>using System;
  
  using Android.App;
  using Android.Content.PM;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Android.OS;
  
  namespace HelloWorld.Droid
  {
      [Activity(Label = "HelloWorld", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
      public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
      {
          public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
          {
              Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
          }
  
          protected override void OnCreate(Bundle savedInstanceState)
          {
              TabLayoutResource = Resource.Layout.Tabbar;
              ToolbarResource = Resource.Layout.Toolbar;
  
              base.OnCreate(savedInstanceState);
              global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
              LoadApplication(new App());
          }
      }
  }
  </code></pre>
  
  <p><em>AndroidManifest.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.gedons.HelloWorld"&gt;
      &lt;uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" /&gt;
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;application android:label="HelloWorld.Android"&gt;
          &lt;provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"&gt;
              &lt;meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /&gt;
          &lt;/provider&gt;
      &lt;/application&gt;
  &lt;/manifest&gt; 
  </code></pre>
  
  <p>_file_path.xml_</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-files-path name="my_images" path="Pictures" /&gt;
      &lt;external-files-path name="my_movies" path="Movies" /&gt;
  &lt;/paths&gt;
  </code></pre>