Skip to content

Instantly share code, notes, and snippets.

@Char0394
Last active June 15, 2017 02:10
Show Gist options
  • Save Char0394/5655350817ec58263c1ab93653d8e54b to your computer and use it in GitHub Desktop.
Save Char0394/5655350817ec58263c1ab93653d8e54b to your computer and use it in GitHub Desktop.
[assembly: Xamarin.Forms.Dependency(typeof(MediaService))]
namespace SelectMultiIpleImagesApp.Droid
{
public class MediaService : Java.Lang.Object, IMediaService
{
public async Task OpenGallery()
{
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.Storage);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Plugin.Permissions.Abstractions.Permission.Storage))
{
Toast.MakeText(Xamarin.Forms.Forms.Context, "Need Storage permission to access to your photos.", ToastLength.Long).Show();
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Plugin.Permissions.Abstractions.Permission.Storage });
status = results[Plugin.Permissions.Abstractions.Permission.Storage];
}
if (status == PermissionStatus.Granted)
{
Toast.MakeText(Xamarin.Forms.Forms.Context, "Select max 20 images", ToastLength.Long).Show();
var imageIntent = new Intent(
Intent.ActionPick);
imageIntent.SetType("image/*");
imageIntent.PutExtra(Intent.ExtraAllowMultiple, true);
imageIntent.SetAction(Intent.ActionGetContent);
((Activity)Forms.Context).StartActivityForResult(
Intent.CreateChooser(imageIntent, "Select photo"), MainActivity.OPENGALLERYCODE);
}
else if (status != PermissionStatus.Unknown)
{
Toast.MakeText(Xamarin.Forms.Forms.Context, "Permission Denied. Can not continue, try again.", ToastLength.Long).Show();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Toast.MakeText(Xamarin.Forms.Forms.Context, "Error. Can not continue, try again.", ToastLength.Long).Show();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment