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?
You can try this,
or
You can show pdf in webview.
You can use Webview to open PDF from url like this:
You can use following library for android:- //in app level build compile
in your xml:
in your activity/ fragment:
For more information, refer this link
Your problem is that Your pdf is downloading inside the Data/data folder and when you try to open using default intent then external app doesn't have any permissions to open the file from inside the data/data folder so you need to download the file outside directory of the app and then try to reopen it will be working.
Simple add following library for android:
for more info, use this GitHub link: https://github.com/JoanZapata/android-pdfview
Using an intent to open a pdf file with
https://
protocol, definitellyhttps://
isn´t the problem.I see that you are trying this method defining the data type:
but it will cause:
if you use this other method probably you can´t see PDF readers in the options to open this kind of files:
You must try this method without the type definition and it will work perfectly:
Other cause of this problem opening the pdf file via intent would be the default application to open this kind of files, so probably you will have a corrupt or invalid application configured, so reset the defaults:
Go to
Settings
. TapApplications
. TapDefault Applications
.Select the app and Clear defaults