Usage

Once you have enabled the required features that you are going to use in Media Services, you can start using the API by importing the namespace.

Using VoxelBusters.EssentialKit;

Once after importing the namespace, all features within Media Services can be accessed through MediaServices class.

Before accessing any gallery features, you need to check if your app have access to gallery.

Get read status

GalleryAccessStatus readAccessStatus   = MediaServices.GetGalleryAccessStatus(GalleryAccessMode.Read);

Get read/write status

GalleryAccessStatus readWriteAccessStatus   = MediaServices.GetGalleryAccessStatus(GalleryAccessMode.ReadWrite);

Once you know the status and you don't have the permission, you can request the access. This request will popup a dialog to let user allow the permission.

MediaServices.RequestGalleryAccess(GalleryAccessMode.Read, callback: (result, error) =>
{
    Debug.Log("Request for gallery access finished.");
    Debug.Log("Gallery access status: " + result.AccessStatus);
});
MediaServices.RequestGalleryAccess(GalleryAccessMode.ReadWrite, callback: (result, error) =>
{
    Debug.Log("Request for gallery access finished.");
    Debug.Log("Gallery access status: " + result.AccessStatus);
});

Read camera access

MediaServices.RequestCameraAccess(callback: (result, error) =>
{
    Debug.Log("Request for camera access finished.");
    Debug.Log("Camera access status: " + result.AccessStatus);
});

For getting the images from gallery, you need read access(GalleryAccessmode.Read) to gallery. Once you have the access, you can call SelectImageFromGallery which can return the texture data in the callback.

MediaServices.SelectImageFromGallery(canEdit: true, (textureData, error) =>
{
    if (error == null)
    {
        Debug.Log("Select image from gallery finished successfully.");
        //textureData.GetTexture() // This returns the texture
    }
    else
    {
        Debug.Log("Select image from gallery failed with error. Error: " + error);
    }
});

canEdit flag will be ignored on Android where as on iOS it opens up an option to edit the image allowing it to crop.

For capturing an image from device camera, you need camera permission which can be gained with RequestCameraAccess method. Once you have the permission, you can call CaptureImageFromCamera method.

MediaServices.CaptureImageFromCamera(true, (textureData, error) =>
{
    if (error == null)
    {
        Debug.Log("Capture image using camera finished successfully.");
    }
    else
    {
        Debug.Log("Capture image using camera failed with error. Error: " + error);
    }
});

For fetching the images with permission, you can use utility methods SelectImageFromGalleryWithUserPermision and CaptureImageFromCameraWithUserPermision.

You need to have read-write access for saving images to gallery. For requesting access you need to call RequestGalleryAccess with GalleryAccessmode.Read mode. Once you have the permission, you need to call SaveImageToGallery by passing the image texture.

Texture2D texture; // Set the texture before calling
MediaServices.SaveImageToGallery(texture, (result, error) =>
{
    if (error == null)
    {
        Debug.Log("Save image to gallery finished successfully.");
    }
    else
    {
        Debug.Log("Save image to gallery failed with error. Error: " + error);
    }
});

Last updated