-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuHelpers.JavaTypes.pas
More file actions
94 lines (83 loc) · 3.1 KB
/
uHelpers.JavaTypes.pas
File metadata and controls
94 lines (83 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
unit uHelpers.JavaTypes;
interface
uses
FMX.Graphics,
System.SysUtils,
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText,
System.Messaging,
Androidapi.JNI.App,
FMX.Surfaces,
FMX.Helpers.Android;
type
THelpsJavaTypes = class
private
class var MessageSubscriptionID: Integer;
class var BitmapResult: TBitmap;
class var ProcFinishHandle: TProc;
class procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
public
class procedure GetImgToIntent(TImage: TBitmap; ProcFinish: TProc);
end;
implementation
const
REQUEST_CODE_VALUE: Integer = 2345;
class procedure THelpsJavaTypes.GetImgToIntent(TImage: TBitmap; ProcFinish: TProc);
var
Intent: JIntent;
begin
BitmapResult := TImage;
ProcFinishHandle := ProcFinish;
MessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage);
Intent := TJIntent.JavaClass.init;
Intent.setType(StringToJString('image/*'));
Intent.setAction(TJIntent.JavaClass.ACTION_GET_CONTENT);
Intent.setPackage(StringToJString('com.google.android.apps.photos'));
TAndroidHelper.Activity.startActivityForResult(Intent, REQUEST_CODE_VALUE);
{ Nos casos a seguir, selecione no aplicativo que lida com fotos Intent.setAction(TJIntent.JavaClass.ACTION_PICK);}
end;
class procedure THelpsJavaTypes.HandleActivityMessage(const Sender: TObject; const M: TMessage);
var
RequestCode, ResultCode: Integer;
Data: JIntent;
InputStream: JInputStream;
JavaBitmap: JBitmap;
BitmapSurface: TBitmapSurface;
begin
try
if M is TMessageResultNotification then
begin
RequestCode := TMessageResultNotification(M).RequestCode;
ResultCode := TMessageResultNotification(M).ResultCode;
Data := TMessageResultNotification(M).Value;
// Liberar manipulador de mensagem
TMessageManager.DefaultManager.Unsubscribe(
TMessageResultNotification, MessageSubscriptionID);
// Com o código de solicitação passado / Se o código de solicitação de retorno corresponder
if RequestCode = REQUEST_CODE_VALUE then
begin
if ResultCode = TJActivity.JavaClass.RESULT_OK then
begin
if Assigned(Data) then
begin
InputStream := TAndroidHelper.Activity.getContentResolver.openInputStream(Data.getData);
// Converter para formato Java Bitmap
JavaBitmap := TJBitmapFactory.JavaClass.decodeStream(InputStream);
// Converter do formato Java Bitmap para BitmapSurface
BitmapSurface := TBitmapSurface.Create;
JBitmapToSurface(JavaBitmap, BitmapSurface);
//Image1.Bitmap.Assign(BitmapSurface);
BitmapResult.Assign(BitmapSurface);
ProcFinishHandle;
end;
end;
end;
end;
except
//as vezes quando a imagem esta danificada o aplicativo
//não consegue processar e fecha, esse try e exatamente para isso
//se a img estiver imperfeita cai aqui.
end;
end;
end.