Jump to content
Colin S

Get path and GPS from a gallery picture for Android

Recommended Posts

How can I select a picture from Android gallery and get the path + GPS data (lat & long)?

 

I get a path to a cached picture, without GPS data.

/data/user/0/com.embarcadero.Project1/cache/IMG_20190319_[...].jpg

The original picture is in /DCIM/Camera/ and has another name.

 

procedure TForm2.FormCreate(Sender: TObject);
begin
  TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedImagePath, DidReceiveBitmap);
end;

procedure TForm2.TakePhotoFromLibraryAction1DidCancelTaking;
begin
  //
end;

procedure TForm2.TakePhotoFromLibraryAction1DidFinishTaking(Image: TBitmap);
begin
  Image1.Bitmap := Image;
end;

procedure TForm2.DidReceiveBitmap(const Sender: TObject; const M: TMessage);
var
  LMessage: TMessageResultNotification;
  LImagePath: String;
begin
  if M is TMessageReceivedImagePath then
  begin
    LMessage := TMessageResultNotification(M);
    LImagePath := (M as TMessageReceivedImagePath).Value; // <--- '/data/user/0/com.embarcadero.Project1/cache/IMG_20190319_[...].jpg'
    if LMessage.RequestCode = TJFMXMediaLibrary.JavaClass.ACTION_TAKE_IMAGE_FROM_LIBRARY then
    begin
      GetEXIF(LImagePath);
      Image2.Bitmap.LoadFromFile(LImagePath);
    end;
  end;
end;

procedure TForm2.GetEXIF(const AFileName: String);
var
  LEXIF: JExifInterface;
  LLatLong: TJavaArray<Single>;
begin
  LEXIF := TJExifInterface.JavaClass.init(StringToJString(AFileName));
  Memo1.Lines.Add('File: ' + AFileName);
  LLatLong := TJavaArray<Single>.Create(2);
  if LEXIF.getLatLong(LLatLong) then
  begin
    Memo1.Lines.Add('Latitude: ' + LLatLong.Items[0].ToString);
    Memo1.Lines.Add('Longitude: ' + LLatLong.Items[1].ToString);
  end;
  LLatLong.Free;
end; 

 

Share this post


Link to post
6 hours ago, Colin S said:

How can I select a picture from Android gallery and get the path + GPS data (lat & long)?

 

I get a path to a cached picture, without GPS data.

/data/user/0/com.embarcadero.Project1/cache/IMG_20190319_[...].jpg

The original picture is in /DCIM/Camera/ and has another name.

When you capture an image to the library, FireMonkey does not give you access to the final image that is stored in the library, as you have noticed.  And there is no guarantee that the image will have GPS information in it anyway (the user can disable that in the phone settings).  So, generally, what you are asking for is not possible with TTakePhotoFromLibraryAction.  You will likely have to either monitor the phone's library for new files, or else just access the phone's Camera and GPS APIs directly,

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×