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(u
If API >=21 you can use PDFRenderer to create a bitmap of each page, but its only viewable, not editable. Here is an example i made up on the fly, lacking navigation buttons, but those shouldn't be to hard to implement.
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File("/path/to/file.pdf"),
ParcelFileDescriptor.MODE_READ_ONLY));
PdfRenderer.Page page = renderer.openPage(0);
Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
Bitmap.Config.ARGB_8888);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
imageView.setImageBitmap(bitmap);
page.close();
renderer.close();
Edit
PdfRenderer requires a local file for the FileDescriptor. So in turn viewing through the "cloud", to my knowledge, isnt possible with this approach.
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)
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);
Simple add following library for android:
//in app level build
compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
//inside your xml file
<com.joanzapata.pdfview.PDFView
android:id="@+id/pdfview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
//inside java code
pdfView.fromAsset(pdfName)
.pages(0, 2, 1, 3, 3, 3)
.defaultPage(1)
.showMinimap(false)
.enableSwipe(true)
.onDraw(onDrawListener)
.onLoad(onLoadCompleteListener)
.onPageChange(onPageChangeListener)
.load();
for more info, use this GitHub link: https://github.com/JoanZapata/android-pdfview
You can use Webview to open PDF from url like this:
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + your url);
Using an intent to open a pdf file with https://
protocol, definitelly https://
isn´t the problem.
I see that you are trying this method defining the data type:
Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(intent);
but it will cause:
ActivityNotFoundException: No Activity found to handle Intent
if you use this other method probably you can´t see PDF readers in the options to open this kind of files:
Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
Intent chooserIntent = Intent.createChooser(intent, "Open Report");
startActivity(chooserIntent);
You must try this method without the type definition and it will work perfectly:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
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
.
Tap Applications
.
Tap Default Applications
.
Select the app and Clear defaults