Xamarin.MediaGallery.Permision.Maui 2.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xamarin.MediaGallery.Permision.Maui --version 2.1.2                
NuGet\Install-Package Xamarin.MediaGallery.Permision.Maui -Version 2.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xamarin.MediaGallery.Permision.Maui" Version="2.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xamarin.MediaGallery.Permision.Maui --version 2.1.2                
#r "nuget: Xamarin.MediaGallery.Permision.Maui, 2.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Xamarin.MediaGallery.Permision.Maui as a Cake Addin
#addin nuget:?package=Xamarin.MediaGallery.Permision.Maui&version=2.1.2

// Install Xamarin.MediaGallery.Permision.Maui as a Cake Tool
#tool nuget:?package=Xamarin.MediaGallery.Permision.Maui&version=2.1.2                

Xamarin.MediaGallery

header

NuGet Badge NuGet downloads license Xamarin.MediaGallery on fuget.org YouTube Video Views

This plugin is designed for picking and saving photos and video files from the native gallery of Android and iOS devices and capture photo.

Unfortunately, at the time of the release of this plugin, MediaPlugin by @jamesmontemagno is no longer supported, and Xamarin.Essentials has not received updates for about 2 months. This plugin has fixed bugs and added some features that are missing in Xamarin.Essentials. I hope that in the future it will be ported to MAUI so that developers have an easy way to add these features to their apps.

"Buy Me A Coffee"

Available Platforms

Platform Minimum OS Version
Android 5.0
iOS 11.0

TargetFrameworks

  • Xamarin.iOS10, net6.0-ios
  • MonoAndroid10.0, MonoAndroid11.0, net6.0-android
  • netstandard2.0, net6.0

Getting started

You can just watch the Video that @jfversluis published

Migration to 2.X.X version

Android

In the Android project's MainLauncher or any Activity that is launched, this plugin must be initialized in the OnCreate method:

protected override void OnCreate(Bundle savedInstanceState)
{
    //...
    base.OnCreate(savedInstanceState);
    NativeMedia.Platform.Init(this, savedInstanceState);
    //...
}

iOS (Optional)

In the iOS project's AppDelegate that is launched, this plugin can be initialized in the FinishedLaunching method:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    NativeMedia.Platform.Init(GetTopViewController);
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());
    return base.FinishedLaunching(app, options);
}

public UIViewController GetTopViewController()
{
    var vc = UIApplication.SharedApplication.KeyWindow.RootViewController;

    if (vc is UINavigationController navController)
        vc = navController.ViewControllers.Last();

    return vc;
}

PickAsync

This method does not require requesting permissions from users

var cts = new CancellationTokenSource();
IMediaFile[] files = null;

try
{
    var request = new MediaPickRequest(1, MediaFileType.Image, MediaFileType.Video)
    {
        PresentationSourceBounds = System.Drawing.Rectangle.Empty,
        UseCreateChooser = true,
        Title = "Select"
    };

    cts.CancelAfter(TimeSpan.FromMinutes(5));

    var results = await MediaGallery.PickAsync(request, cts.Token);
    files = results?.Files?.ToArray();
}
catch (OperationCanceledException)
{
    // handling a cancellation request
}
catch (Exception)
{
    // handling other exceptions
}
finally
{
    cts.Dispose();
}


if (files == null)
    return;

foreach (var file in files)
{
    var fileName = file.NameWithoutExtension; //Can return an null or empty value
    var extension = file.Extension;
    var contentType = file.ContentType;
    using var stream = await file.OpenReadAsync();
    //...
    file.Dispose();
}

This method has two overloads:

  • Task<MediaPickResult> PickAsync(int selectionLimit = 1, params MediaFileType[] types)
  • Task<MediaPickResult> PickAsync(MediaPickRequest request, CancellationToken token = default)

Android

To handle runtime results on Android, this plugin must receive any OnActivityResult.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
   if (NativeMedia.Platform.CheckCanProcessResult(requestCode, resultCode, intent))
   NativeMedia.Platform.OnActivityResult(requestCode, resultCode, intent);
   
   base.OnActivityResult(requestCode, resultCode, intent);
}
  • When using the PickAsync method the selectionLimit parameter just sets multiple pick allowed
  • A request to cancel PickAsync method will cancel a task, but the picker UI can remain open until it is closed by the user
  • The use of Title property depends on each device
  • UseCreateChooser property has not been used since version 2.0.0

iOS

  • Multi picking is supported since iOS version 14.0+ On older versions, the plugin will prompt the user to select a single file
  • The NameWithoutExtension property on iOS versions before 14 returns a null value if the permission to access photos was not granted
  • Title property not used
  • UseCreateChooser property not used
Presentation Location

When picking files on iPadOS you have the ability to present in a pop over control. This specifies where the pop over will appear and point an arrow directly to. You can specify the location using the PresentationSourceBounds property. Setting this property has the same behavior as Launcher or Share in Xamarin.Essentials.

PresentationSourceBounds property takes System.Drawing.Rectangle for Xamarin or Microsoft.Maui.Graphics.Rectangle for .net6(MAUI)

Screenshots:

Сapture Photo with Metadata

//...
if (!MediaGallery.CheckCapturePhotoSupport())
    return;

var status = await Permissions.RequestAsync<Permissions.Camera>();

if (status != PermissionStatus.Granted)
    return;

using var file = await MediaGallery.CapturePhotoAsync()

NameWithoutExtension will always return $"IMG_{DateTime.Now:yyyyMMdd_HHmmss}" value.

Android

Open the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />

If Camera is not required in your application, you can specify false.

<queries>
  <intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
  </intent>
</queries>

iOS

In your Info.plist add the following keys:

<key>NSCameraUsageDescription</key>
<string>This app needs access to the camera to take photos.</string>

SaveAsync

//...
var status = await Permissions.RequestAsync<SaveMediaPermission>();

if (status != PermissionStatus.Granted)
    return;

await MediaGallery.SaveAsync(MediaFileType.Video, filePath);

//OR Using a byte array or a stream

await MediaGallery.SaveAsync(MediaFileType.Image, stream, fileName);

//The name or the path to the saved file must contain the extension.

//...

Permission

Add Xamarin.MediaGallery.Permision or Xamarin.MediaGallery.Permision.Maui nuget package to use the SaveMediaPermission

Android

Open the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node.


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • When saving media files, the date and time are appended to the file name

iOS

In your Info.plist add the following keys:


<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery for saving photos and videos</string>


<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to the photo gallery for saving photos and videos</string>

Screenshots

______________ iOS Android ______________
iOS Android
Product Compatible and additional computed target framework versions.
.NET net6.0-android31.0 is compatible.  net6.0-ios16.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net8.0-android was computed.  net8.0-ios was computed. 
MonoAndroid monoandroid10.0 is compatible.  monoandroid11.0 is compatible.  monoandroid12.0 is compatible. 
Xamarin.iOS xamarinios10 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Xamarin.MediaGallery.Permision.Maui:

Repository Stars
dimonovdd/Xamarin.MediaGallery
This plugin is designed to picking and save images and video files from native gallery of Android and iOS devices and capture photos
Version Downloads Last updated
2.2.1 129,522 2/24/2023
2.2.1-preview 138 2/17/2023
2.2.0 6,992 1/6/2023
2.2.0-preview 198 12/6/2022
2.1.2 615 11/28/2022
2.1.2-alpha003 121 11/27/2022
2.1.2-alpha002 132 11/26/2022
2.1.2-alpha001 122 11/23/2022
2.0.0 831 12/5/2021
2.0.0-preview4 153 12/4/2021
2.0.0-preview3 5,192 11/23/2021
2.0.0-preview2 149 11/22/2021