Alexander Toman Alexander Toman - 19 days ago 6
Java Question

On displaying PDF and modifying the extension of Uri

I am building a social application for smart phones using Firebase for my database.

The app primal feature is Portable Document Format (PDF), where users can upload, download and prerferably view PDFs in the most optimized and sufficient way.

My current code on both platforms ( Android + iOS ) is functional. Users can easily interact with the three main features discussed earlier. Images,videos,maps and pdfs are downloadable, uploadable and viewable

However in the Android version I have suddenly encountered a serious rendering issue with my PDFs. Which is preventing the Android users from viewing PDFs.

After three frustrating days and after using every PdfRendering project there is, I am sadly still unsuccessful at tackling this problem.

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

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

String normal_pdf_link_from_the_web = "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);


Then the result is succesful

image -SUCCESSSFULLY- displaying a normal PDF Url

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%2F8828?alt=media&token=fab355da-47a6-4a27-894f-40798590a89a";
String googleDocs = "https://docs.google.com/viewer?url=";

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


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

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

If I render the same Firebase link on the iOS everything works fine
tinypic.com/r/2ivzcy9/9

Which means the browser -Safari is able to render the PDF and not -Chrome.

Users can still download the firebase link, yet not able to view.

This is how I am fetching the PDF content from the device

Intent intentPDF = new Intent(Intent.ACTION_GET_CONTENT);
intentPDF.setType("application/pdf");
startActivityForResult(intentPDF, GALLERY_REQUEST);


This is how I am receiving the intent content and assigning it to my PdfUri

@Override
public void onActivityResult(int requestCode, int resultCode, Intent oIntent) {


if (resultCode != RESULT_OK) {
return;

} else {

PdfUri = oIntent.getData();
}

}


}

Here is how I am shipping the PdfUri to both my database and storage at Firebase

private void startPosting() {

if (mPdfUri != null)) {

StorageReference filepath = mStorage.child(PdfUri.getLastPathSegment().toString());

filepath.putFile(PdfUri).addOnSuccessListener(new OnSuccess..


I have attempted to modifiy the Uri by adding the extension ".pdf" However I was faced with "type unidentified".

//1 first get IntentData and assign to Uri
PdfUri = oIntent.getData();
//2 second convert Uri to string to add extension
String uriString = (PdfUri.toString())+ ".pdf";
//3 reconvert String to Uri to create a Uri PDF link
mPdfUri = Uri.parse(uriString);


Many thanks for your insights and assistants

I am very well aware that using Webview in rendering PDF is bad practice. This was meant for demonstration purposes only.

The solution suggested by the user sphippen solved this question. Using the UrlEncoder on the firebase link, The pdf was successfully render. 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")
Comments