Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limitations on opening pdf file in Android

Tags:

android

pdf

I am trying to opening some pdf from my Android application. I am using an Intent for doing that:

Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(intent);

This code works well for some pdf but it fails when I try to open others.

This is the message that Android is showing to me:

There is a problem with the file.

I have to mention that the pdf that are being opened without problems are created with one Crystal Report template and the pdfs that are failing are created with another one.

As opposed, if I open the url of the pdfs that are failing on my browser (on my computer), it does not give to me any error opening them so I guess that maybe there is some limitation on Android that differs from some pdf to another (on Crystal Report template) but I cannot see it.

What limitations exist on opening a pdf file on Android? (Size, some parameters of Crystal Report that are not allowed, etc...)

I have discarded that it could be a size limitation because the pdf files that are giving problems are smaller than the files that do not give any error.

Proves I have done:

  • Opening wrong PDFs on browser ~~> OK
  • Downloading wrong PDF on mobile phone and open it ~~> OK
  • Opening wrong PDFs on APP ~~> Error
  • Opening good PDF on APP of the company that PDFs crash ~~> OK

EDIT

I have noticed that I was using http:// protocol but the PDF is on a https:// protocol, so I have changed it on Uri.parse method.

When I made this change, the app crashed and an error was shown on the log:

android.content.ActivityNotFoundException: No Activity found to handle Intent

Also, I have noticed that the PDFs that does not give to me any error, are in an url with http:// protocol instead of https:// so I guess that https:// protocol can be the problem.

Am I only able to open http:// request on an Intent?

like image 493
Francisco Romero Avatar asked Jul 25 '17 08:07

Francisco Romero


People also ask

Why can't I open PDF files on my Android phone?

To fix a PDF file not opening in Adobe reader, you will need to download the latest version of Adobe Reader. After which you will disable the protected mode that comes with it by default. Once this is changed, the issue of the PDF file not opening in Adobe reader will be resolved.

What is the limitation of PDF?

Because the maximum size limit for filed PDF documents is 10 MB, it may be necessary, at times, to split a PDF into multiple documents in order to file a large document. This can be done with Adobe's Extract Pages function.

Can Android phones open PDF files?

Open and read PDFs on Android. Download and install Acrobat Reader from the Google Play Store. Launch the app. On the bottom menu bar, select Files. Locate your PDF file on your Android and select it.

Why are my PDFs not opening?

Here are some of the most common culprits to consider: Your laptop doesn't have a PDF reader installed. Your PDF reader or preferred program is out of date and needs an update. Your PDF application is potentially damaged or needs to be rebooted.


2 Answers

It could be the file failed to be interpret by the Android PDF viewer app. Have you tried to copy/download the exact same file to your Android phone and open from there?

Also, I'd suggest to use IntentChooser for launching the PDF viewer, just to play safe on no PDF viewer installed / giving user option to choose app:

Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
Intent chooserIntent = Intent.createChooser(intent, "Open Report");
startActivity(chooserIntent);
like image 123
Jack Cheung Avatar answered Oct 19 '22 03:10

Jack Cheung


I found a workaround to view my PDF on my Android application (but does not allow me to download it after show on the application). If I open my PDF using Google Docs I can open it with my Intent.

This is what I had to add before my url:

https://docs.google.com/gview?embedded=true&url=

so the final url will be like:

https://docs.google.com/gview?embedded=true&url=https://myUrl.pdf

Here is the whole code I am using right now:

String url= "https://docs.google.com/gview?embedded=true&url=https://url.pdf";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

But it is still not enough, because I would like to open it without need of using a third party application. Also, opening PDF with Google Docs is slow and I have to wait too much until the PDF is finally opened.

If anyone knows how to open it with native Android please let me know.


What happens if I do not open it with Google Docs?

With the same code, but just using my url, instead the added Google Docs url, Android let me choose between two applications: Adobe Acrobat Reader and Google Chrome.

  • If I open it with Google Chrome it download the PDF but it is not opened automatically.

  • If I open it with Adobe Acrobat Reader, it gives to me the following error:

    The PDF cannot be shown (it cannot be opened)

like image 44
Francisco Romero Avatar answered Oct 19 '22 01:10

Francisco Romero