Jump to content
Sign in to follow this  
iken

How to get the result of Activity.startActivityForResult in android

Recommended Posts

Open the photo album in TForm1.imagesClick:
var
  Intent: JIntent;

begin

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_PICK);
  Intent.setType(StringToJString('image/*'));

  TAndroidHelper.Activity.getPackageManager.resolveActivity(Intent, 0);

  TAndroidHelper.Activity.startActivityForResult(Intent, 0, nil);

end

 

It also defines TForm1.onActivityResult(RequestCode, ResultCode: Integer; Data: JIntent);

to reading the result of the intent. But as a result, I can open the photo album and select the picture, but onActivityResult is not run after selecting the picture.

Share this post


Link to post
34 minutes ago, iken said:

But as a result, I can open the photo album and select the picture, but onActivityResult is not run after selecting the picture.

You need to do it this way, for example:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Messaging,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure MessageResultNotificationMessageHandler(const Sender: TObject; const M: TMessage);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses
  Androidapi.Helpers, Androidapi.JNI.App, Androidapi.JNI.GraphicsContentViewText;

const
  cRequestCode = 1234; // Just a random number

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, MessageResultNotificationMessageHandler);
end;

destructor TForm1.Destroy;
begin
  TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, MessageResultNotificationMessageHandler);
  inherited;
end;

procedure TForm1.MessageResultNotificationMessageHandler(const Sender: TObject; const M: TMessage);
var
  LMessage: TMessageResultNotification;
begin
  LMessage := TMessageResultNotification(M);
  if LMessage.RequestCode = cRequestCode then
  begin
    if LMessage.ResultCode = TJActivity.JavaClass.RESULT_OK then
    begin
      // Here, handle the Intent that is the LMessage.Value property
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  LIntent: JIntent;
begin
  LIntent := TJIntent.Create;
  LIntent.setAction(TJIntent.JavaClass.ACTION_PICK);
  LIntent.setType(StringToJString('image/*'));
  TAndroidHelper.Activity.startActivityForResult(LIntent, cRequestCode);
end;

end

 

  • Like 1

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
Sign in to follow this  

×