我的应用从服务器下载文件,并将文件保存在默认的下载目录中,下载后如何打开文件查看。
try
MS := TMemoryStream.Create;
try
Http.Request.BasicAuthentication:= true;
Http.Request.Username := '*****';
Http.Request.Password := '****';
Http.Get('http://'+serverIP+':8080/api/getpdf/'+fType, MS);
MS.Position:=0;
{$IFDEF MSWINDOWS}
MS.SaveToFile(fl);
{$ENDIF}
{$IFDEF LINUX}
MS.SaveToFile(fl);
{$ENDIF}
{$IFDEF ANDROID}
if PermissionsService.IsPermissionGranted(JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE)) then
begin
MS.SaveToFile(fl);
end;
{$ENDIF}
previewFile(fl,fType);<--------------
finally
MS.Free;
end;
finally
Http.Free;
end;
在windows平台上,我做到了。
ShellExecute(0, 'open',PChar(fl),nil,nil,0)
Linux平台有什么建议吗?
谢谢 sddk 我最终采用了这个解决方案,在Ubuntu 19.1下效果很好。
procedure TfrmMain.previewFile(fl,fType:string);
var
a:string;
M : TMarshaller;
{$IFDEF ANDROID}
Intent : JIntent;
URI : Jnet_Uri;
{$ENDIF}
begin
AniIndicatorItems.Enabled:=false;
AniIndicatorItems.Visible:=false;
rcMaskItems.Visible:=false;
TabItem1.Repaint;
{$IFDEF MSWINDOWS}
ShellExecute(0, 'open',PChar(fl),nil,nil,0) ;
{$ENDIF}
{$IFDEF LINUX}
fl:='xdg-open '+fl;//<-------------------------------------
popen(M.AsAnsi(PWideChar(fl)).ToPointer,'r');//<-----------
{$ENDIF}
{$IFDEF ANDROID}
if PermissionsService.IsPermissionGranted(JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE)) then
begin
if fType='1' then//pdf
OpenPDF(fl)
else//excel
OpenXLSX(fl);
end;
{$ENDIF}
end;
//------------------------------------------------------------------------------
{$IFDEF ANDROID}
procedure OpenPDF(const AFileName: string);
var
LIntent: JIntent;
LAuthority: JString;
LUri: Jnet_Uri;
begin
LAuthority := StringToJString(JStringToString(TAndroidHelper.Context.getApplicationContext.getPackageName) + '.fileprovider');
LUri := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context, LAuthority, TJFile.JavaClass.init(StringToJString(AFileName)));
LIntent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW);
LIntent.setDataAndType(LUri, StringToJString('application/pdf'));
LIntent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
TAndroidHelper.Activity.startActivity(LIntent);
end;
//------------------------------------------------------------------------------
procedure OpenXLSX(const AFileName: string);
var
LIntent: JIntent;
LAuthority: JString;
LUri: Jnet_Uri;
begin
LAuthority := StringToJString(JStringToString(TAndroidHelper.Context.getApplicationContext.getPackageName) + '.fileprovider');
LUri := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context, LAuthority, TJFile.JavaClass.init(StringToJString(AFileName)));
LIntent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW);
LIntent.setDataAndType(LUri, StringToJString('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'));
LIntent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
TAndroidHelper.Activity.startActivity(LIntent);
end;
{$ENDIF}
对于安卓,你也需要这个。DW.Androidapi.JNI.FileProvider.pas。