Xamarin.MediaGallery 2.1.0-preview1

This is a prerelease version of Xamarin.MediaGallery.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Xamarin.MediaGallery --version 2.1.0-preview1                
NuGet\Install-Package Xamarin.MediaGallery -Version 2.1.0-preview1                
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" Version="2.1.0-preview1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xamarin.MediaGallery --version 2.1.0-preview1                
#r "nuget: Xamarin.MediaGallery, 2.1.0-preview1"                
#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 as a Cake Addin
#addin nuget:?package=Xamarin.MediaGallery&version=2.1.0-preview1&prerelease

// Install Xamarin.MediaGallery as a Cake Tool
#tool nuget:?package=Xamarin.MediaGallery&version=2.1.0-preview1&prerelease                

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.

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 specifies whether to use Intent.CreateChooser

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:

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 net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-android31.0 is compatible.  net6.0-ios was computed.  net6.0-ios15.0 is compatible.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed.  monoandroid10.0 is compatible.  monoandroid11.0 is compatible. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed.  xamarinios10 is compatible. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Xamarin.MediaGallery:

Package Downloads
BitooBitImageEditorFix

Simple image editor for Xamarin Forms

GitHub repositories (1)

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

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
3.0.0-preview 80 11/5/2024
2.2.1 243,487 2/24/2023
2.2.1-preview 181 2/17/2023
2.2.0 60,929 1/6/2023
2.2.0-preview 288 12/6/2022
2.1.2 29,897 11/28/2022
2.1.2-alpha003 157 11/27/2022
2.1.2-alpha002 178 11/26/2022
2.1.2-alpha001 159 11/23/2022
2.1.1 131,786 12/18/2021
2.1.0 375 12/15/2021
2.1.0-preview1 694 12/12/2021
2.0.0 499 12/5/2021
2.0.0-preview4 169 12/4/2021
2.0.0-preview3 5,256 11/23/2021
2.0.0-preview2 296 11/22/2021
2.0.0-preview1 348 10/25/2021
2.0.0-preview 165 10/24/2021
1.0.2.2 16,481 9/22/2021
1.0.2.2-preview1 256 9/15/2021
1.0.2.1 4,583 8/19/2021
1.0.2 9,848 7/12/2021
1.0.2-preview2 259 7/9/2021
1.0.2-preview1 373 7/4/2021
1.0.1 1,253 6/25/2021
1.0.1-preview4 315 6/22/2021
1.0.1-preview3 3,920 6/3/2021
1.0.1-preview2 355 5/30/2021
1.0.1-preview1 233 5/28/2021
1.0.0 4,355 5/2/2021
1.0.0-alpha005 282 5/2/2021
1.0.0-alpha004 251 4/29/2021
1.0.0-alpha003 241 4/27/2021
1.0.0-alpha002 284 4/18/2021
1.0.0-alpha001 248 4/11/2021