{"id":13710010,"url":"https://github.com/dimonovdd/Xamarin.MediaGallery","last_synced_at":"2025-05-06T18:33:43.796Z","repository":{"id":44771697,"uuid":"356916596","full_name":"dimonovdd/Xamarin.MediaGallery","owner":"dimonovdd","description":"This plugin is designed to picking and save images and video files from native gallery of Android and iOS devices and capture photos","archived":false,"fork":false,"pushed_at":"2025-01-30T18:33:24.000Z","size":10838,"stargazers_count":160,"open_issues_count":8,"forks_count":21,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-05-05T08:56:41.539Z","etag":null,"topics":["android","capture-photo","dotnet","essentials","hacktoberfest","image","maui","media","mediagallery","mediapicker","net-ios","saving-photos","video","xamarin","xamarin-android","xamarin-forms","xamarin-ios"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dimonovdd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-11T16:15:15.000Z","updated_at":"2025-04-18T09:10:19.000Z","dependencies_parsed_at":"2024-10-26T13:17:18.881Z","dependency_job_id":"db8bd4b0-47f4-4819-bfe6-56e8471c1ac4","html_url":"https://github.com/dimonovdd/Xamarin.MediaGallery","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimonovdd%2FXamarin.MediaGallery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimonovdd%2FXamarin.MediaGallery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimonovdd%2FXamarin.MediaGallery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimonovdd%2FXamarin.MediaGallery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dimonovdd","download_url":"https://codeload.github.com/dimonovdd/Xamarin.MediaGallery/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252744866,"owners_count":21797695,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","capture-photo","dotnet","essentials","hacktoberfest","image","maui","media","mediagallery","mediapicker","net-ios","saving-photos","video","xamarin","xamarin-android","xamarin-forms","xamarin-ios"],"created_at":"2024-08-02T23:00:50.488Z","updated_at":"2025-05-06T18:33:38.781Z","avatar_url":"https://github.com/dimonovdd.png","language":"C#","funding_links":["https://www.buymeacoffee.com/dimonovdd"],"categories":["Plugins"],"sub_categories":[],"readme":"# MediaGallery for .NET Android and iOS\n\n![header](./header.svg)\n\n[![NuGet Badge](https://img.shields.io/nuget/vpre/Xamarin.MediaGallery)](https://www.nuget.org/packages/Xamarin.MediaGallery/) [![NuGet downloads](https://img.shields.io/nuget/dt/Xamarin.MediaGallery)](https://www.nuget.org/packages/Xamarin.MediaGallery/) [![license](https://img.shields.io/github/license/dimonovdd/Xamarin.MediaGallery)](./LICENSE) [![Xamarin.MediaGallery on fuget.org](https://www.fuget.org/packages/Xamarin.MediaGallery/badge.svg)](https://www.fuget.org/packages/Xamarin.MediaGallery) [![YouTube Video Views](https://img.shields.io/youtube/views/8JvgnlHVyrI?style=social)](https://youtu.be/8JvgnlHVyrI)\n\nThis plugin is designed for picking and saving photos and video files from the native gallery of Android and iOS devices and capture photo. \n\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/dimonovdd)\n\n## FAQ\n\nPlease read this file and see samples before creating an issue.\n\n### Q: Why does the plugin not support Xamarin?\n\n**A:** Yes, it's funny. Initially, I made this plugin as a temporary solution to expand the functionality of Xamarin.Essentials. Xamarin is no longer supported by Microsoft and MAUI still has scant functionality for working with media files. For these reasons, this plugin will continue to exist and support .NET for iOS and Android.  \n\n### Q: [Why does a image have wrong orientation?](https://github.com/dimonovdd/Xamarin.MediaGallery/issues/105)\n\n**A:** This is correct behavior. The plugin returns images without any changes, See [metadata](https://www.nuget.org/packages/MetadataExtractor/)\n\n### Q: [How do I get `FilePath`?](https://github.com/dimonovdd/Xamarin.MediaGallery/issues/104)\n\n**A:** It is not possible. [But you can copy a file to a cache directory](./Sample/Common/src/Helpers/FilesHelper.cs#L8)\n\n### Q: [How does Xamarin.MediaGallery work on a PopupPage?](https://stackoverflow.com/questions/70233374/how-does-xamarin-mediagallery-work-in-popuppage-in-xamarin)\n\n**A:** Fine! But you need to [initialize the plugin](#ios-optional) on iOS. [See taht sample code](https://github.com/xamarin/Essentials/pull/1846#issuecomment-975207765)\n\n### Q: [Why an error thrown when picking a image on a iOS simulator?](https://github.com/dimonovdd/Xamarin.MediaGallery/issues/92)\n\n**A:** This issue is on Apple side\n\n## Available Platforms\n\n| Platform | Minimum OS Version |\n| --- | --- |\n| Android | 5.0 |\n| iOS | 13.6 |\n\n### TargetFrameworks\n\n- `net8.0-ios`\n- `net8.0-android`\n\n## Getting started\n\nYou can just watch the [Video](https://youtu.be/8JvgnlHVyrI) that [@jfversluis](https://github.com/jfversluis) published\n\n[**Migration to 3.X.X version**](./docs/migration3.x.x.md)\n\n~~[**Migration to 2.X.X version**](./docs/migration2.x.x.md)~~\n\n### Android\n\nIn the Android project's MainLauncher or any Activity that is launched, this plugin must be initialized in the OnCreate method:\n\n```csharp\nprotected override void OnCreate(Bundle savedInstanceState)\n{\n    //...\n    base.OnCreate(savedInstanceState);\n    NativeMedia.Platform.Init(this, savedInstanceState);\n    //...\n}\n ```\n\n### iOS (Optional)\n\nIn the iOS project's AppDelegate that is launched, this plugin can be initialized in the FinishedLaunching method:\n\n```csharp\npublic override bool FinishedLaunching(UIApplication app, NSDictionary options)\n{\n    NativeMedia.Platform.Init(GetTopViewController);\n    global::Xamarin.Forms.Forms.Init();\n    LoadApplication(new App());\n    return base.FinishedLaunching(app, options);\n}\n\npublic UIViewController GetTopViewController()\n{\n    var vc = UIApplication.SharedApplication.KeyWindow.RootViewController;\n\n    if (vc is UINavigationController navController)\n        vc = navController.ViewControllers.Last();\n\n    return vc;\n}\n ```\n\n## PickAsync\n\nThis method does not require requesting permissions from users\n\n```csharp\nvar cts = new CancellationTokenSource();\nIMediaFile[] files = null;\n\ntry\n{\n    var request = new MediaPickRequest(1, MediaFileType.Image, MediaFileType.Video)\n    {\n        PresentationSourceBounds = System.Drawing.Rectangle.Empty,\n        Title = \"Select\"\n    };\n\n    cts.CancelAfter(TimeSpan.FromMinutes(5));\n\n    var results = await MediaGallery.PickAsync(request, cts.Token);\n    files = results?.Files?.ToArray();\n}\ncatch (OperationCanceledException)\n{\n    // handling a cancellation request\n}\ncatch (Exception)\n{\n    // handling other exceptions\n}\nfinally\n{\n    cts.Dispose();\n}\n\n\nif (files == null)\n    return;\n\nforeach (var file in files)\n{\n    var fileName = file.NameWithoutExtension; //Can return an null or empty value\n    var extension = file.Extension;\n    var contentType = file.ContentType;\n    using var stream = await file.OpenReadAsync();\n    //...\n    file.Dispose();\n}\n ```\n\n This method has two overloads:\n\n- `Task\u003cMediaPickResult\u003e PickAsync(int selectionLimit = 1, params MediaFileType[] types)`\n- `Task\u003cMediaPickResult\u003e PickAsync(MediaPickRequest request, CancellationToken token = default)`\n\n### Android\n\n To handle runtime results on Android, this plugin must receive any `OnActivityResult`.\n\n ```csharp\nprotected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)\n{\n    if (NativeMedia.Platform.CheckCanProcessResult(requestCode, resultCode, intent))\n    NativeMedia.Platform.OnActivityResult(requestCode, resultCode, intent);\n    \n    base.OnActivityResult(requestCode, resultCode, intent);\n}\n ```\n\n If an app has `android:targetSdkVersion=\"33\"` or greater [new Photo picker](https://developer.android.com/training/data-storage/shared/photopicker) will be used if possible.\n\n#### Default behavior\n\n- When using `PickAsync` method `selectionLimit` parameter just sets multiple pick allowed\n- A request to cancel `PickAsync` method will cancel a task, but the picker UI can remain open until it is closed by the user\n- The use of `Title` property depends on each device\n\n#### Photo Picker behavior\n\n- `selectionLimit` parameter limits the number of selected media files\n- `Title` property not used\n\n### iOS\n\n- Multi picking is supported since iOS version 14.0+ On older versions, the plugin will prompt the user to select a single file\n- The `NameWithoutExtension` property on iOS versions before 14 returns a null value if the permission to access photos was not granted\n- `Title` property not used\n\n#### Presentation Location\n\nWhen 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](https://docs.microsoft.com/en-us/xamarin/essentials/share?tabs=android#presentation-location).\n\n`PresentationSourceBounds` property takes `System.Drawing.Rectangle` for `Xamarin` or `Microsoft.Maui.Graphics.Rect` for `.net6(MAUI)`\n\n**Screenshots:**\n\n- [Popover](./Screenshots/iPadPopover.png)\n- [PageSheet](./Screenshots/iPadPageSheet.png)\n\n## Сapture Photo with Metadata\n\n```csharp\n//...\nif (!MediaGallery.CheckCapturePhotoSupport())\n    return;\n\nvar status = await Permissions.RequestAsync\u003cPermissions.Camera\u003e();\n\nif (status != PermissionStatus.Granted)\n    return;\n\nusing var file = await MediaGallery.CapturePhotoAsync()\n ```\n\n`NameWithoutExtension` will always return `$\"IMG_{DateTime.Now:yyyyMMdd_HHmmss}\"` value.\n\n### Android\n\nOpen the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node.\n\n```xml\n\u003cuses-permission android:name=\"android.permission.CAMERA\" /\u003e\n```\n\n```xml\n\u003cuses-feature android:name=\"android.hardware.camera\" android:required=\"true\" /\u003e\n```\n\nIf Camera is not required in your application, you can specify `false`.\n\n```xml\n\u003cqueries\u003e\n  \u003cintent\u003e\n    \u003caction android:name=\"android.media.action.IMAGE_CAPTURE\" /\u003e\n  \u003c/intent\u003e\n\u003c/queries\u003e\n```\n\n### iOS\n\nIn your `Info.plist` add the following keys:\n\n ```xml\n\u003ckey\u003eNSCameraUsageDescription\u003c/key\u003e\n\u003cstring\u003eThis app needs access to the camera to take photos.\u003c/string\u003e\n ```\n\n## SaveAsync\n\n```csharp\n//...\nvar status = await Permissions.RequestAsync\u003cSaveMediaPermission\u003e();\n\nif (status != PermissionStatus.Granted)\n    return;\n\nawait MediaGallery.SaveAsync(MediaFileType.Video, filePath);\n\n//OR Using a byte array or a stream\n\nawait MediaGallery.SaveAsync(MediaFileType.Image, stream, fileName);\n\n//The name or the path to the saved file must contain the extension.\n\n//...\n ```\n\n### Permission\n\nAdd [`Xamarin.MediaGallery.Permision`](https://www.nuget.org/packages/Xamarin.MediaGallery.Permision) or [`Xamarin.MediaGallery.Permision.Maui`](https://www.nuget.org/packages/Xamarin.MediaGallery.Permision.Maui) nuget package to use the `SaveMediaPermission`\n\n### Android\n\nOpen the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node.\n\n ```xml\n\u003c!-- for saving photo/video --\u003e\n\u003cuses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" /\u003e\n ```\n\n- When saving media files, the date and time are appended to the file name\n\n### iOS\n\nIn your `Info.plist` add the following keys:\n\n ```xml\n\u003c!-- for saving photo/video on iOS 14+ --\u003e\n\u003ckey\u003eNSPhotoLibraryAddUsageDescription\u003c/key\u003e\n\u003cstring\u003eThis app needs access to the photo gallery for saving photos and videos\u003c/string\u003e\n\n\u003c!-- for saving photo/video on older versions --\u003e\n\u003ckey\u003eNSPhotoLibraryUsageDescription\u003c/key\u003e\n\u003cstring\u003eThis app needs access to the photo gallery for saving photos and videos\u003c/string\u003e\n ```\n\n## Screenshots\n\n|   iOS   | Android - Defult  | Android - Photo Picker |\n|:-------:|:-------:|:-------:|\n|![iOS](./Screenshots/ios.jpg)|![Android](./Screenshots/droid.jpg)|![Android2](./Screenshots/droid-33.png)|\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimonovdd%2FXamarin.MediaGallery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdimonovdd%2FXamarin.MediaGallery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimonovdd%2FXamarin.MediaGallery/lists"}