Delphi XE6で作成した選択した画像を表示するAndroidアプリケーションのサンプルです。
インテント(Intent)で画像を選択して、選択された画像をフォームに表示します。
フォームにはTButtonをTImageを配置し、ボタンのOnClickイベントを追加します。
ソースコードは次のようになります。
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.