New FireMonkey Media Library Options in RAD Studio XE8

by Jun 15, 2015

RAD Studio XE8 provides new FireMonkey Media Library options. This includes the ability to automatically save pictures taken by the device camera (in your iOS and Android applications) to the on-device photo library.

Today, I thought I would show you how to save photos taken with the device camera directly to your photo library by extending our ShareSheet code snippet demo that is included with RAD Studio XE8. This demo uses the built-in ShareSheet and TakePhoto actions, and utilizes the the IFMXPhotoLibrary interface for automatically saving the image to the camera roll.

Note: The ShareSheet code snippet can be found in the Mobile Snippets directory on your machine.

Image 

Image2

Image3

unit uMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects, FMX.StdCtrls,
  System.Actions, FMX.ActnList, FMX.StdActns, FMX.MediaLibrary.Actions, FMX.Graphics,
  FMX.Controls.Presentation;

type
  TShareSheetForm = class(TForm)
    ActionList1: TActionList;
    ShowShareSheetAction1: TShowShareSheetAction;
    Action1: TAction;
    TakePhotoFromCameraAction1: TTakePhotoFromCameraAction;
    TopToolbar: TToolBar;
    Label1: TLabel;
    BottomToolbar: TToolBar;
    btnShare: TButton;
    btnTakePhoto: TButton;
    imgCameraPicture: TImage;
    procedure ShowShareSheetAction1BeforeExecute(Sender: TObject);
    procedure TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  ShareSheetForm: TShareSheetForm;

implementation

{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}

uses
FMX.Platform, FMX.MediaLibrary;

procedure TShareSheetForm.ShowShareSheetAction1BeforeExecute(Sender: TObject);
begin
   { Show the Share Sheet }
  ShowShareSheetAction1.Bitmap.Assign(imgCameraPicture.Bitmap);
end;


procedure TShareSheetForm.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
var
  Service: IFMXPhotoLibrary;
begin
   { Assign the picture taken from the camera to the TImage control and save to the Library }
  imgCameraPicture.Bitmap.Assign(Image);
 if TPlatformServices.Current.SupportsPlatformService(IFMXPhotoLibrary, Service) then
    Service.AddImageToSavedPhotosAlbum(imgCameraPicture.Bitmap)
  else
    ShowMessage('The IFMXPhotoLibrary interface is not supported.');
end;
end.

Information from our DocWiki:

If your application handles pictures by using either the FireMonkey Actions or the IFMXCameraService interface, you can use the TCustomTakePhotoAction.NeedSaveToAlbum property or a parameter of the TParamsPhotoQuery type to cause your application to automatically save the pictures to the device Photo Library.
In addition, FireMonkey provides the IFMXPhotoLibrary interface that allows you to save any bitmap image to the device Photo Library.

For more information and code examples, see: