简体   繁体   中英

How to ask authorization for save image in Gallery on Android using Xamarin?

I need to save a screenshot capture in the user gallery but the authorization is not automatically asked. I allowed permission in the Android manifest (WRITE_EXTERNAL_STORAGE) but it doesn't ask it for, I have to set it in the emulator settings. Does someone know how to ask authorization, as the location?

You could use the plugin PermissionsPlugin from nuget .

Usage

try
{
    var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
    if (status != PermissionStatus.Granted)
    {
        if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
        {
            await DisplayAlert("Need location", "Gunna need that location", "OK");
        }

        status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
    }

    if (status == PermissionStatus.Granted)
    {
        //Query permission
    }
    else if (status != PermissionStatus.Unknown)
    {
        //location denied
    }
}
catch (Exception ex)
{
  //Something went wrong
}

For more details you could check https://github.com/jamesmontemagno/PermissionsPlugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM