android intent打开活动,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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值