-
Couldn't load subscription status.
- Fork 28
Open
Labels
Description
What would you like to be added:
The MediaGallery class is a static class designed for interaction with the device's media gallery on iOS and Android platforms. It provides functionality to check user permissions for accessing the gallery and saving media files to the gallery.
Why is this needed:
User requirement to efficiently save files into storage.
Public API
Uno.Toolkit.UI.MediaFileType enum
Represents a media file type.
Members
Image- Image media file type.Video- Video media file type
Uno.Toolkit.UI.MediaGallery class
Allows interaction with the device's media gallery.
This class is available only on Android and iOS targets, where media gallery requires a specific API.
CheckAccessAsync method
/// <summary>
/// Checks the user's permission to access the device's gallery.
/// Will trigger the permission request if not already granted.
/// </summary>
/// <returns>A Task that represents a boolean indicating whether the user has access.</returns>
public static async Task<bool> CheckAccessAsync()
- Description: Asynchronously checks if the application has permission to access the media gallery. If permission is not already granted, it will prompt the user for permission.
- Returns: A
Task<bool>that completes withtrueif access is granted, andfalseotherwise.
SaveAsync (stream)
/// <summary>
/// Saves a media file to the device's gallery.
/// </summary>
/// <param name="type">The type of the media file.</param>
/// <param name="stream">A stream representing the media file.</param>
/// <param name="targetFileName">The name to save the file as.</param>
/// <returns>A Task that represents the progress of the save operation.</returns>
public static async Task SaveAsync(MediaFileType type, Stream stream, string targetFileName)- Description: Asynchronously saves a media file to the device's gallery using a stream.
- Parameters:
MediaFileType type: The type of the media file (image or video).Stream stream: The media file data in a stream.string targetFileName: The desired file name for the saved media.
- Returns: A
Taskthat completes when the save operation is finished.
SaveAsync (byte array)
/// <summary>
/// Saves a media file to the device's gallery.
/// </summary>
/// <param name="type">The type of the media file.</param>
/// <param name="data">A byte array representing the media file.</param>
/// <param name="targetFileName">The name to save the file as.</param>
/// <returns>A Task that represents the progress of the save operation.</returns>
public static async Task SaveAsync(MediaFileType type, byte[] data, string targetFileName)- Description: Asynchronously saves a media file to the device's gallery provided a byte array.
- Parameters:
MediaFileType type: The type of the media file (image or video).byte[] data: The media file data in a byte array.string targetFileName: The desired file name for the saved media.
- Returns: A
Taskthat completes when the save operation is finished.
Usage
// Check for gallery access
bool hasAccess = await MediaGallery.CheckAccessAsync();
if (hasAccess)
{
// Save a video to the gallery using stream
using Stream videoStream = ...; // Video stream
await MediaGallery.SaveAsync(MediaFileType.Video, videoStream, "MyVideo.mp4");
}