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 xe 中的字符串生成哈希值(md5 / sha-1 / jenkins)

Delphi tanimate组件

Delphi在postgresql中读写二进制数据

Delphi 取出鼠百标点击的 stringgrid 中某单度元格的值

Delphi+access错误"不正常地定义参数对象。提供了不一致或不完整的信息。"

Delphi获取pid的父进程文件名

Delphi 上传文件到七牛,纯原生

Delphi 防止程序重复执行的单元

Delphi通过进程名获取进程pid函数

Delphi 调用浏览文件夹 selectdirectory

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



打赏

取消

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

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

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

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

评论

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