Delphi XE6 使用Intent启动活动并在Android应用程序中获取返回值的示例


本文整理自网络,侵删。

 
源代码如下所示:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
  FMX.Dialogs, FMX.StdCtrls, FMX.Layouts, FMX.Memo, FMX.Objects,
  //↓追加
  System.Messaging, Androidapi.JNI.GraphicsContentViewText;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
  private
    { private 宣言 }
    FMessageSubscriptionID: Integer;
    procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
    function OnActivityResult(RequestCode, ResultCode: Integer;
      Data: JIntent): Boolean;
  public
    { public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses
  Androidapi.Helpers,
  Androidapi.JNI.App,
  Androidapi.JNI.JavaTypes,
  FMX.Helpers.Android,
  FMX.Platform.Android,
  FMX.Surfaces;

const
  ImageRequestCode: Integer = 123;

procedure TForm1.Button1Click(Sender: TObject);
var
  Intent: JIntent;
begin
  FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage
    (TMessageResultNotification, HandleActivityMessage);

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_PICK);
  Intent.setType(StringToJString('image/*'));
  SharedActivity.startActivityForResult(Intent, ImageRequestCode);
end;

procedure TForm1.HandleActivityMessage(const Sender: TObject;
  const M: TMessage);
begin
  if M is TMessageResultNotification then
  begin
    OnActivityResult(TMessageResultNotification(M).RequestCode,
      TMessageResultNotification(M).ResultCode,
      TMessageResultNotification(M).Value);
  end;
end;

function TForm1.OnActivityResult(RequestCode, ResultCode: Integer;
  Data: JIntent): Boolean;
var
  InputStream: JInputStream;
  NativeBitmap: JBitmap;
  Bitmap: TBitmapSurface;
begin
  TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification,
    FMessageSubscriptionID);
  FMessageSubscriptionID := 0;

  if RequestCode <> ImageRequestCode then
    Exit(False);

  Result := True;
  if ResultCode <> TJActivity.JavaClass.RESULT_OK then
    Exit;
  if not Assigned(Data) then
    Exit;

  InputStream := SharedActivity.getContentResolver.openInputStream
    (Data.getData);

  NativeBitmap := TJBitmapFactory.JavaClass.decodeStream(InputStream);
  Bitmap := TBitmapSurface.Create;
  if JBitmapToSurface(NativeBitmap, Bitmap) then
  begin
    Image1.Bitmap.Assign(Bitmap);
  end;
end;

end.

相关阅读 >>

Delphi 二进制转换为文本

Delphi 修改内存

Delphi关于邮箱收信的问题

Delphi 获取某个页面所有的链接

Delphi winapi: getclassname - 获取指定窗口的类名

Delphi 随便找个网站获取格林威治时间, 并转换到北京时间

Delphi制作外挂的操作技巧

Delphi发送邮件源代码

Delphi工具之tdump

Delphi fdconnection查看所有表包含用户表和系统表

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...