Open pdf in external app - android

I have and android app. It has a pdf in assets folder. I written a code to view this pdf in external app(like adobe pdf viewer). It is working fine but problem is that there is Share option in Adobe pdf viewer. I dont want that user can distribute this pdf.
My problem is that there is an important pdf book and I want user can read only but can not share or redistribute it. Please give an idea. I will change my code.
I am using following code:
Uri path = Uri.fromFile(new File(str,
"lemmelibroandroid.pdf"));
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);

I dont think it is possible to limit the capabilities of the external application used to open the pdf
Have a look at
http://asmncl.blogspot.in/2012/06/android-open-pdf-file-in-webview.html to see how to open the pdf in a WebView

It is not possible,so the best option here will be to show your content in your own app.
I am sure that you will find a proper way to display it some how in activity.
Maybe you should change the format of PDF to HTML and it will be easier to show.

Related

Different way to open a PDF than from current SD?

I know there are many similar questions, but I need specifics. I originally wanted my app to open PDFS within the app itself, but I have settled to send an intent activity to adobe reader. I am currently opening PDFS by looking for the file on the device itself. Is there a way I can have the PDFS in my app, and create a folder on the users device, and then look for them? or something similar? Obvisouly the user isn't going to have the PDF already installed on their device. Here is my current code.
Intent intent7 = new Intent();
intent7.setPackage("com.adobe.reader");
intent7.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/Download/Auto-example.pdf")), "application/pdf");
startActivity(intent7);
Is there a way I can have the PDFS in my app, and create a folder on the users device, and then look for them?
You can put the PDF in internal storage (e.g., getFilesDir()), then use FileProvider to serve them via a ContentProvider. This sample project demonstrates serving a PDF from internal storage (copied there from assets/) and viewing it in the user's chosen PDF viewer. There is also an Android training module covering this.
With respect to the code that you have, please use ACTION_VIEW as your Intent action (e.g., pass that to the constructor) and delete the setPackage() line.

Cannot open pdf document within android app

I was wondering can anyone help me with a problem that I'm having. I've looked at many posts of the same issue but I still cannot resolve this problem. I have created a PDF document. I have placed my created PDF file into my res/raw folder (I have also tried placing in assets folder) and I'm trying to open it within my app. When I click the button, it opens the dialog that I can select to complete the action using adobe reader, amazon kindle or polaris office. But when I go to try and open the pdf, I get an error saying "The document cannot be opened because it is not a valid PDF document".
The following is the code I am using to try and open the pdf, has anyone any ideas on why this will not work?
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.user_manual_v1_0_0;
File file = new File(fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
It will not work because few, if any, PDF viewers are set up to open PDFs from resources, and none can read directly from assets.
You will be better served copying the file to the filesystem. If you copy it to external storage, you can just open up a PDF viewer on that. If you copy it to internal storage -- by default, private to your app -- you can create a ContentProvider to serve the PDF to the viewer. This sample application demonstrates the latter approach.
Be sure to not forget the permissions for your app to reach external files. Place these two lines in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

pdf viewer library for android

I know this question has been asked several times but I couldnt find one satisfying answer.My requirement is simple.just to open the pdf docs within my app(android).But I am not able to find a simple way of doing this.either its native c/c++ way or writing your own engine.But isnt there any simple API so that I can include it like a jar and view the pdf?
No. There is no API in the Android SDK to natively display PDF.
Solutions I can suggest:
-Use an external application:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
-If your PDF document is accessible online, use the Google Docs Viewer to open your PDF in a WebView:
http://docs.google.com/viewer?url=http://mypdf.pdf
you can use native plugin to show the PDF within your app. try this apv

How do I display a PDF?

I have tried to display a PDF file by using a WebView using the following code -
webview.loadUrl("http://www.mywebsite.co.uk/floorplan.pdf");
or
Uri uri = Uri.parse("http://www.mywebsite.co.uk/floorplan.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Using either I cannot get the PDF to display unless I run it through Adobe (so my app is downloading it), which then looks fine. How can I get the PDF to display using Adobe through my app or am I better off displaying the PDF as an image instead?
You'd have to build a pdf viewer to display it inside your app. Your best best is to launch the Intent like you are doing to display it in an external pdf viewer.
Check out this question for more.
Show PDF in Android

Display PDF in webview

I want to show a pdf in my application. My webservice gives me the pdf and I want to download it to sd card and then I want show that pdf in my app through webview.
I was also struggling with Android PDF showing problem for some time. I did the same approach as you mentioned with downloading the PDF to the SD card, but I did not manage to open it up otherwise than using a preinstalled Android App which could do the PDF opening (e.g. Adobe Reader or similar). I think that it's not possible to view the PDF in a WebView widget. The reason is simply that the included webview does not support plugins like adobe reader (see: http://osdir.com/ml/Android-Developers/2010-09/msg03331.html)
You can easily open up the downloaded PDF using an Intent call:
File file = new File("/sdcard/filename.pdf");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Due to the fact that this technique opens up the PDF viewer directly after my application makes this call, the back button takes the user directly back to the app, so it feels like the pdf viewing is part of my app.
Hope this helps, best regards
As already answered here, the WebView can't render PDFs. You can do what it is said in the accepted answer in this question and try to open the PDF with the default PDF viewer in the device. If the device has no PDF viewer installed, a message will be displayed to the user.

Categories

Resources