iken 0 Posted November 19, 2024 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
Dave Nottage 606 Posted November 19, 2024 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 1 Share this post Link to post
Alisson 0 Posted 14 hours ago (edited) I managed to make it work, but at some points my app restarts. The process is: MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_VIEW); TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage); AIntent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW); AIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK); AIntent.setData(TJNet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(AURI)))); TAndroidHelper.Activity.startActivity(AIntent); So far the second app opens correctly. But after I save it and return to my first app before entering the 'HandleActivityMessage' the app restarts. Any tips? This doesn't happen all the time, but when it does I lose the intent return data. Edited 14 hours ago by Alisson Share this post Link to post
Remy Lebeau 1573 Posted 14 hours ago 15 minutes ago, Alisson said: TAndroidHelper.Activity.startActivity(AIntent); ... before entering the 'HandleActivityMessage'... You are not going to get a notification if you use startActivity(), you need to use startActivityForResult(). 1 Share this post Link to post
Dave Nottage 606 Posted 8 hours ago 6 hours ago, Alisson said: I managed to make it work, but at some points my app restarts. You appear to be trying to do something different from the OP. 6 hours ago, Alisson said: But after I save it Save what? 6 hours ago, Alisson said: Any tips? Yes: please describe what it is you are actually trying to achieve, preferably with a more complete example. Share this post Link to post