Skip to Content
Author's profile photo Bill Froelich

Viewing PDFs in Agentry on Android

A recent post on SCN asked about opening a local PDF in Android using Agentry (http://scn.sap.com/message/14526817).  As it turns out, after some additional testing the Android HTML browsers don’t have the ability to directly view PDF files like the iOS HTML control.

However, if you have an appropriate PDF viewer installer on your Android device (like Adobe Reader or Polaris Office) then those applications will register an intent filter with the OS so that when an attempt is made to view a PDF file the OS will automatically hand off the request to the installed application.  This works great as long as the application opening the file can determine and set the data type of the file being opened.

It was suggested that we could use the Agentry URL action step to cause the OS to invoke the intent using a file:///path/to/pdf/file.pdf command.  From the responses it appears this works for some users but in my case it doesn’t work (at least for my current device – Samsung Galaxy S4).  However, since that option didn’t work for me I had to look at another option.  My solution was to create a helper application to open the PDFs that is able to detect and set the appropriate MIME type so that the OS can prompt or open the appropriate application to view the PDF (Polaris Office in my case).

I created an android application that I so originally named External Document Launcher.  My application registers a custom scheme (extdoclaunch) that accepts a single parameter of the path to the file to open.  I use the OpenURL action step and create a rule to build the command.

openURLrule.png

My app receives the URL and based on the file extension builds the appropriate intent with the file and data type before invoking the file.  The Data property referened in the rule points to the External Data Property on the attached document.

Since the OS now knows the type of the file it appropriately opens or prompts the user on what application they want to use.  Since I have to know the appropriate MIME type to set I have a set list of application types that are currently supported (PDF, XLS, XLSX, DOC, DOCX, PPT, PPTX, MP3, TXT, PNG, GIF, JPG, JPEG).

To use the application simply install the .apk on your device and create the openURL action step similar to the above rule.  When you run the action selecting one of the supported documents you should be prompted or the document should automatically open.  If the file extension is not one of the supported types  a popup message will be displayed saying “Unsupported Document Type”.

To download the apk visit this link (no Google account is needed to download the file).

https://drive.google.com/file/d/0B0mxpban3HyIVjJZX0ZCOS1YQmM/edit?usp=sharing

I’ll work on a follow-up post with more details of the andorid application internals if there is more interest.

Enjoy!

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Rikardt Louw
      Rikardt Louw

      Hi Bill , Thanks its awesome!

      You can use Content links as well. If you don't want to use a App.

      PDF:

      content://com.adobe.reader.fileprovider/storage/sdcard0/Download/a.pdf?application/pdf

      HTML:

      content://com.android.htmlfileprovider/storage/sdcard0/Download/index.html?text/html

      Author's profile photo Former Member
      Former Member

      Hi Bill,

      Thanks for the application and it works beautifully. I am able to view PDF, docs but when i see for jpg and png it says unsupported format. We need some other formats like tiff. Kindly advice on how we can extend your application for multiple file formats?

      Author's profile photo Bill Froelich
      Bill Froelich
      Blog Post Author

      Is the error saying "Unsupported Document Type" or unsupported format?  Just trying to understand where the error is coming from.

      In terms of graphics support the built in HTML control can be used to view JPG, PNG and GIF files without needing the helper application.  I do not know if TIFF is supported in the HTML control (or in the gallery viewer app).

      --Bill

      Author's profile photo Former Member
      Former Member

      Hey Bill,

      We are trying to do this at a client I am working for, but cannot seem to figure out how to implement the rule correctly.  Do we have to implement the rule to the OpenURL action?  Does that question make sense?

      I would love to pick your brain to get this working.

      Thanks,

      -Joe

      Author's profile photo Bill Froelich
      Bill Froelich
      Blog Post Author

      Joe,

      Please create a Discussion marked as a Question this way you can mark it correct when answered.  Not only will others benefit from your solution once it is found, but you will have more folks looking to provide a solution when your issue has the greater visibility of a Discussion.

      --Bill

      Author's profile photo Pathik Chitania
      Pathik Chitania

      If anyone is still having trouble opening pdfs after applying this fix, be sure to confirm that the attachment download location specified in the Work Manager application is a publicly accessible location.The default location that is specified in the application is the Application Directory inside the Work Manager application. 3rd party applications don’t have access to this location and hence, can’t open the pdf file. The solution here is to change this directory setting to a public location (such as Downloads) on the Android Device. Here are the steps to make this change –

      1. Open the Work Manager application definitions in Eclipse.
      2. Go to Modules -> “PM” Module -> Objects -> DocumentLink Object.
      3. Go to the “FileData” External Data Property and go to the File Locations tab.
      4. Click on the Base Path dropdown in the Android section and select a public location such as Downloads.
      5. You can choose to create a sub-directory by providing a directory name in the Relative Path field. The sub-directory will be created automatically if it doesn’t exist.
      6. Save and publish these changes to your server as you normally would.

      There are a couple of things to keep in mind when making this change –

      1. The directory change will apply to all types of attachments, not just pdfs.
      2. There is a security risk with storing files with sensitive information in public locations. So, be sure to delete these files after use if they contain sensitive information.

      Hope this helps!

      Author's profile photo Nazarene Madriaga
      Nazarene Madriaga

      Do we have an option to edit the PDF when opening from Work Manager and save it after which will upload back up to SAP?