Gianluca Colombo
Gianluca Colombo

Reputation: 819

Add photo to gallery with firemonkey

I'm trying to save pictures on external storage of my device then add the image to gallery. I tried the following code but it doesn't work! No errors are occured, but I can't see the picture saved in gallery.

function GetImageUri(ABitmap: TBitmap): Jnet_Uri;
var
  ImageFile: JFile;
  ImageUri: Jnet_Uri;
  FileNameTemp: JString;
  FileNameExt: JString;
begin
  FileNameTemp := StringToJString('temp');
  FileNameExt := StringToJString('.jpg');
  try
    ImageFile := TJFile.JavaClass.createTempFile(FileNameTemp, FileNameExt);
    ImageUri := TJnet_Uri.JavaClass.fromFile(ImageFile);
    showmessage(JStringToString(ImageFile.getAbsolutePath));
    ABitmap.SaveToFile(JStringToString(ImageFile.getAbsolutePath));
  finally
    Result := ImageUri;
  end;
end;

procedure AddPhotoToGallery(const APhoto: TBitmap);
var
  Intent: JIntent;
begin
  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_MEDIA_SCANNER_SCAN_FILE);
  Intent.setData(GetImageUri(APhoto));
  SharedActivity.sendBroadcast(Intent);

end;

Upvotes: 2

Views: 1216

Answers (1)

Agustin Ortu
Agustin Ortu

Reputation: 240

Use the IFMXPhotoLibrary platform service. This interface has a AddImageToSavedPhotosAlbum method you can call with a TBitmap object to add to the device gallery

Check this snippet:

uses
  FMX.Platform,
  FMX.MediaLibrary;

procedure TForm2.Button1Click(Sender: TObject);
var
  Gallery: IFMXPhotoLibrary;
begin
  if not TPlatformServices.Current.SupportsPlatformService(IFMXPhotoLibrary, Gallery) then
    Exit;

  Gallery.AddImageToSavedPhotosAlbum(YoutBitmapObjectHere);
end;

Upvotes: 2

Related Questions