How to open pdf file from server without saving it on device and without using any third party application.because i don't want my user to download any application to use my apps.And i don't want to use web view for opening pdf file.
Asked
Active
Viewed 3.2k times
3 Answers
17
This method worked in the older versions of android:
In a new activity:
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=URL_of_PDF/fileName.pdf");
setContentView(webview);
give internet
permission in manifest file.
USE the following method to open a PDF file with already installed 3rd party application.
To open the PDF in application use:
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);

PrincessLeiha
- 3,144
- 4
- 32
- 53
-
i don't want to view my pdf in webview.. so please suggest me how can i open pdf in application. – Sudhanshu Srivastava May 07 '12 at 11:32
-
see, in your question, you said that you don't want any 3rd party application, so `WEBVIEW` is the way out. In the URL you can directly pass the URL of that website where the PDF exists. If you know that the 3rd party application is allowed then you can go for the `intents` in the sample above. – PrincessLeiha May 07 '12 at 11:41
-
for using Intent i have to store pdf file on local but i don't to store it on local. can i use some jar file to view the pdf ?? – Sudhanshu Srivastava May 07 '12 at 11:52
-
Why don't you want to open it in WEBVIEW? For webview, it's not necessary to have the PDF file on the local drive. – PrincessLeiha May 07 '12 at 12:12
-
actually on opening pdf in webView the quality of my pdf is decreased. google doc decreases the quality of documents and it is very slow too – Sudhanshu Srivastava May 07 '12 at 12:14
-
the slowness would depend on the internet connection speed. Any ways, these are the methods I have worked with. No jar or any other app i know. – PrincessLeiha May 07 '12 at 12:26
-
thanx for your all supports. if i get the solution of my problem then i will surly came back to u – Sudhanshu Srivastava May 07 '12 at 13:03
-
`setPluginsEnabled(true)` not available for me – Pratik Butani Dec 23 '13 at 05:11
-
@PratikButani just checked the docs! API 18 onwards plugins not supported... We need to find a work around for that now.. – PrincessLeiha Jan 03 '14 at 03:22
-
what i have to do now? any solution? – Pratik Butani Jan 03 '14 at 04:18
-
@PratikButani you may use the code sample given by AgarwalShankar. There are a few flaws, but atleast it will work. – PrincessLeiha Jan 03 '14 at 05:39
3
use PdfViewer.jar and making a code like below - copied from here
First.java
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File images = Environment.getExternalStorageDirectory();
imagelist = images.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return ((name.endsWith(".pdf")));
}
});
pdflist = new String[imagelist.length];
for(int i = 0;i<imagelist.length;i++)
{
pdflist[i] = imagelist[i].getName();
}
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String path = imagelist[(int)id].getAbsolutePath();
openPdfIntent(path);
}
private void openPdfIntent(String path)
{
try
{
final Intent intent = new Intent(First.this, Second.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
}
Second.java
public class Second extends PdfViewerActivity
{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public int getPreviousPageImageResource() {
return R.drawable.left_arrow;
}
public int getNextPageImageResource() {
return R.drawable.right_arrow;
}
public int getZoomInImageResource() {
return R.drawable.zoom_in;
}
public int getZoomOutImageResource() {
return R.drawable.zoom_out;
}
public int getPdfPasswordLayoutResource() {
return R.layout.pdf_file_password;
}
public int getPdfPageNumberResource() {
return R.layout.dialog_pagenumber;
}
public int getPdfPasswordEditField() {
return R.id.etPassword;
}
public int getPdfPasswordOkButton() {
return R.id.btOK;
}
public int getPdfPasswordExitButton() {
return R.id.btExit;
}
public int getPdfPageNumberEditField() {
return R.id.pagenum_edit;
}
}
Hope this helps you lot. Try this. Don't forgot to add your Second.java in your manifest. Add some drawables whatever it requires in second.java with your drawables. And, Refer the example from GitHub

Community
- 1
- 1

Shankar Agarwal
- 34,573
- 7
- 66
- 64
-
Hello Agarwal ! I was able to display pdf file in device but this library was unable to load pdf files of larger size and also the images inside the pdf file are displayed as blurry images. Have you faced this issue ?? – KK_07k11A0585 Aug 27 '12 at 05:34
-
Once I got this working, I was dissappointed by the lack of a smooth interface and lack of gestures and multitude of nio.BufferUnderflowExceptions, even after adding android:largeHeap="true" to my
tag in the Manifest – James Perih Nov 26 '13 at 18:35
0
for this u have to use pdf reader libraries.this link will help u
https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android

Community
- 1
- 1

Jackson Chengalai
- 3,907
- 1
- 24
- 39
-
i can't understand how to use the library in my application. i am very new to android so please help me. – Sudhanshu Srivastava May 07 '12 at 11:34
-
check this link this will give u a brief idea about adding library into your project http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project – Jackson Chengalai May 07 '12 at 12:00
-
if u dont want to store the pdf in local then u should use the webview meathod... – Jackson Chengalai May 07 '12 at 12:03