MediaServices.RequestCameraAccess(callback: (result, error) =>
{
Debug.Log("Request for camera access finished.");
Debug.Log("Camera access status: " + result.AccessStatus);
});
Get images from Gallery or Camera
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.
Save image to gallery
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);
}
});