acarlomagno
acarlomagno

Reputation: 79

Delphi open PDF from ios/Android local storage

I tried to open a PDF file on mobile device from local storage.

I have put the file to:

StartUp\Documents\ for iOS

assets\internal\ for Android

with this code:

filename := TPath.Combine(TPath.GetDocumentsPath, 'file.pdf');
WebBrowser1.Navigate('file://' + filename);

on iOS works fine ... open the pdf correctly

on Android ... nothing .. no error .. no pdf ... If I try to open a html file .. works fine.

Any suggestions ?

Antonello

Upvotes: 1

Views: 12997

Answers (2)

Thomas_K
Thomas_K

Reputation: 11

It is possible to use the Android PDFView component via the JNI birdge in XE7. https://github.com/JoanZapata/android-pdfview The Java2op tool can generate the needed interface pas file and the IDE can manage the changes in the classes.dx file, that is the easy part. Bring it to live in Delphi requires a little bit of work, but it is doable. For this you can almost take the code 1:1 from the delphi "webview" implementation which can find in following two classes TCustomWebBrowser in the FMX.Webbrowser.pas file and TAndroidWebBrowserService in FMX.WebBrowser.Android.pas file. For my proof of concept i did write a TPDFViewAndroid class which is derived from TControl, using codes from both mention classes. So i am in the process getting the needed permission from Embarcaderro for a public release, but even before this i have to find the time to clean up, make a real component, examples ...

Upvotes: 1

Afzal Ali
Afzal Ali

Reputation: 822

{$IFDEF ANDROID}
Uses
   Androidapi.JNI.GraphicsContentViewText,
   Androidapi.Helpers,
   Androidapi.JNI.JavaTypes,
   Androidapi.JNI.Net;
{$ENDIF}

procedure btnPdfClick(Sender: TObject);
var
   fName       : String;
{$IFDEF ANDROID}
   Intent      : JIntent;
   URI         : Jnet_Uri;
{$ENDIF}
begin
    fName := TPath.GetSharedDownloadsPath + PathDelim + 'test.pdf';

    {$IFDEF ANDROID}
      URI := TJnet_Uri.JavaClass.parse(StringToJString('file:///' + fName));
      intent := TJIntent.Create;
      intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
      intent.setDataAndType(URI,StringToJString('application/pdf'));
      SharedActivity.startActivity(intent);
    {$ENDIF}
end;

Upvotes: 6

Related Questions