Skip to content

gaelian/Xamarin.MediaGallery

 
 

Repository files navigation

Xamarin.MediaGallery

header

NuGet Badge NuGet downloads license

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.

Available Platforms

Platform Version Minimum OS Version
Android MonoAndroid 10.0+ 5.0
iOS Xamarin.iOS10 11.0
.NET Standard 2.0 -

Getting started

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);
    Xamarin.MediaGallery.Platform.Init(this, savedInstanceState);
    //...
}

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

protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
   if (Xamarin.MediaGallery.Platform.CheckCanProcessResult(requestCode, resultCode, intent))
   Xamarin.MediaGallery.Platform.OnActivityResult(requestCode, resultCode, intent);
   
   base.OnActivityResult(requestCode, resultCode, intent);
}

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

<!-- for saving photo/video -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

iOS

In your Info.plist add the following keys:

<!-- for saving photo/video on iOS 14+ -->
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery for saving photos and videos</string>

<!-- for saving photo/video on older versions -->
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to the photo gallery for saving photos and videos</string>

PickAsync

//...
var results = await MediaGallery.PickAsync(1, MediaFileType.Image, MediaFileType.Video);

if (results?.Files == null)
    return;

foreach(var res in results.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();
//...
}

SaveAsync

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

if (status != PermissionStatus.Granted)
    return;

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

Platform Differences

Android

  • When saving media files, the date and time are appended to the file name
  • When using PickAsync method selectionLimit parameter just sets multiple pick allowed

iOS

  • Multi picking is supported from iOS version 14.0+ On older versions, the plugin will prompt the user to select a single file

Screenshots

______________ iOS Android ______________
iOS Android

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%