Alexander Toman Alexander Toman - 20 days ago 10
Java Question

On rendering PDF and modifying Uri

I am trying to render PDF link using a webview. The link I am trying to render, is uploaded by my app to the firebase database.

While the link is rendered succesffuly on the iOS devices. It isn't on the Android.

I greatly speculate the reason behind all this confusion to be the firebase link .

If I try to render a normal pdf link from the web.

String pdf_sample = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
String googleDocs = "https://docs.google.com/viewer?url=";

Webviewz.getSettings().setJavaScriptEnabled(true);
Webviewz.loadUrl(googleDocs + pdf_sample);


Resulting in succesful PDF rendering

image -SUCCESSSFULLY- displaying a normal PDF Url

If however I rely and attempt to display and render a PDF link from my Firebase database, I get this blank dark gray background by google docs and a sign of NO PREVIEW AVAILABLE

String Firebase_link_failure = "https://firebasestorage.googleapis.com/v0/b/jouska-aabee.appspot.com/o/PDF_files%2F305?alt=media&token=b9cf2fa6-f6ff-4a3b-8908-9eac294c4668";
String googleDocs = "https://docs.google.com/viewer?url=";

Webviewz.getSettings().setJavaScriptEnabled(true);
Webviewz.loadUrl(googleDocs + Firebase_link_failure);


Resulting in No Preview Available

image -FAILURE- displaying a Firebase PDF link on android devices


The solution suggested by the user sphippen worked out. Using the UrlEncoder on the firebase link, The pdf was successfully rendered. Below is the single modification made.


Webviewz.loadUrl(googleDocs+ URLEncoder.encode(firebase_link, "utf-8"));

Answer

It looks like the problem is that you're just appending two strings to form your URL:

Webviewz.loadUrl(googleDocs + pdf_sample);

Looking at the full URL (using the values from your code sample):

https://docs.google.com/viewer?url=https://firebasestorage.googleapis.com/v0/b/jouska-aabee.appspot.com/o/PDF_files%2F8828?alt=media&token=fab355da-47a6-4a27-894f-40798590a89a

The & character after alt=media ends the url parameter, so the URL the page tries to access is just https://firebasestorage.googleapis.com/v0/b/jouska-aabee.appspot.com/o/PDF_files%2F8828?alt=media, which doesn't contain the download token.

You'll need to escape the Firebase Storage download link for use as a URL parameter (replacing & with %26, ? with %3F, % with %25, etc.). The URLEncoder class should work:

URLEncoder.encode(pdf_sample, "UTF-8")