Волобуев, вот вам меч.
type
TfgOpenDialog = class
private
FProc: TProc<boolean>;
FMultiSelect: boolean;
FFiles: TStrings;
FMimeType: String;
procedure ResultCallback(const Sender: TObject; const M: TMessage);
procedure SetMultiSelect(const Value: boolean);
procedure SetMimeType(const Value: String);
public
property Files:TStrings read FFiles;
property MultiSelect:boolean read FMultiSelect write SetMultiSelect;
property MimeType:String read FMimeType write SetMimeType;
constructor Create;
destructor Destroy; override;
procedure Execute(Proc: TProc<boolean>);
end;
constructor TfgOpenDialog.Create;
begin
FFiles:=TStringList.Create;
TMessageManager.DefaultManager.SubscribeToMessage(TfgActivityResultMessage, ResultCallback);
end;
destructor TfgOpenDialog.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TfgActivityResultMessage, ResultCallback);
FFiles.Free;
end;
procedure TfgOpenDialog.ResultCallback(const Sender: TObject; const M: TMessage);
var
J:TJUri;
function getFileName(Uri:TJUri):String;
var c:JCursor;
begin
result:='';
if (uri.getScheme().equalsIgnoreCase(StringToJString('content')))
then
begin
c:=TfgAndroidHelper.Context.getContentResolver.query(uri,nil,nil,nil,nil);
try
if (c<>nil) and c.moveToFirst then
result:=JStringToString(c.getString(c.getColumnIndex(StringToJString('_display_name') ) ))
finally
if (c<>nil) then
c.close;
end;
end;
if (result ='') then
result:=JStringToString(uri.getPath());
end;
procedure UriToFile(uri:TJUri);
var i:integer;
bt:byte;
b:TJavaArray<Byte>;
jis:TJInputStream;
FilePath: string;
ms:TMemoryStream;
begin
ms := TMemoryStream.Create;
try
jis := TfgAndroidHelper.Context.getContentResolver.openInputStream(uri);
b := TJavaArray<Byte>.Create(jis.available);
jis.read(b);
for i:=0 to B.Length-1 do
begin
bt:=b.Items[i];
ms.Write(bt,1);
end;
jis.close;
FilePath:=System.IOUtils.TPath.GetTempFileName+'.jpg';
ms.SaveToFile(FilePath);
FFiles.Add(getFileName(uri)+'='+FilePath);
finally
ms.Free;
end;
end;
var i:integer;
begin
if TfgActivityResultMessage(M).RequestCode = 42 then
begin
if TfgActivityResultMessage(M).ResultCode = TJActivity.RESULT_OK then
begin
if FMultiSelect then
for I := 0 to TfgActivityResultMessage(M).Data.getClipData().getItemCount-1 do
UriToFile(TfgActivityResultMessage(M).Data.getClipData().getItemAt(i).getUri)
else
UriToFile(TfgActivityResultMessage(M).Data.getData());
FProc(True);
end
else
FProc(False)
end;
end;
procedure TfgOpenDialog.SetMimeType(const Value: String);
begin
FMimeType := Value;
end;
procedure TfgOpenDialog.SetMultiSelect(const Value: boolean);
begin
FMultiSelect := Value;
end;
procedure TfgOpenDialog.Execute(Proc: TProc<boolean>);
var
Intent: TJIntent;
begin
FProc:=Proc;
Intent:=TJIntent.Create;
if FMultiSelect then
Intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
else
Intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
Intent.setAction(TJIntent.ACTION_GET_CONTENT);
Intent.setType(StringToJString(MimeType));
TfgAndroidHelper.Activity.startActivityForResult(Intent,42);
end;
Пример использования
fg:=TfgOpenDialog.Create;
fg.MimeType:='image/*';
fg.Execute(procedure(B:boolean)
begin
if B and (fg.Files.Count>0) then
begin
TfgAssetsManager.Current.AddBitmapFromFile(fg.Files.Names[0],fg.Files.ValueFromIndex[0]);
fgImage1.ImageName:=fg.Files.Names[0];
fgLabel1.Text:=fgImage1.ImageName;
end;
fg:=nil;
end)
Спасибо, не пинайте за небрежность кода