Read pdf file from sd-card with the help of webview - android

Is it possible to read pdf file stored in my sd card to view in web view.
I tried using
WebView webview = new WebView(myactivity.this);
webview.getSettings().setJavaScriptEnabled(true);
File file = new File(Environment.getExternalStorageDirectory()
+ "/pdf" + "/ypc.pdf");
Uri uri = Uri.fromFile(file);
webview.loadUrl(uri.toString());
but got no results,. is any other way possible

I think PDF reader is not in-built with Android-WebView or any web-browser. it is a plugin to web-browser like Adobe PDF Reader with our PC browsers (Mozilla, Chrome, Opera, etc.)
So you must configure external PDF Reader app with Android-WebView, to read PDF files with webview.
Otherwise try Google Docs Viewer to read PDF file, but you must have internet connection and set appropriate permissions to your app.
Replace last line of your code with below line.
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + uri.toString());
I hope it may help you.

Related

ANDROID - How to open a pdf url which does not have a .pdf extension?

I am trying to open a url which is actually a pdf file but it does not have .pdf extension in android webview BUT it does not works. It directly downloads. But when i open the url in WEB browser, the pdf file opens perfectly.
Since this url does not have .pdf extension, I can not use google docs as well. Also tried to download and open the file using any pdf viewer app, it also fails. The main reason is my Url does not have .pdf extension. Is there any other solution?
I think you are not setting the content type as pdf(MIME). Thats why you are getting problem when you are downloading the pdf content.
See this post for more details.
Proper MIME media type for PDF files
MIME TYPES
Render a PDF file using Java on Android
you can see this .pdf extension is not necessary

How to add text as prefix

I wanted to open a file without downloading it so i used window.open in InAppBrowser and it works fine in ios... But i am not able to open a pdf file this way on android.
I was able to open a pdf file using google doc by prefixing like my url:
https://docs.google.com/gview?embedded=true&url=<my_url>
Now its opening pdf files, but files with images (.png) or text (.txt) cannot be opened this way, because they will be shown as html files.
So, when i get a pdf file on an android device it should check if the last three characters are matching (pdf) and in that case add a prefix to open the pdf file. All other files should work fine.
How about:
String url = "my_pdf.pdf";
if(url.endsWith(".pdf")){
url = "https://docs.google.com/gview?embedded=true&url=" + url;
}

Open pdf in external app

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.

PdfReader in Android

I have to develop an application which reads pdf file in pdf format and only pdf file should be read in the application.I wanted to know one more thing is it possible to use itext.jar for
the android application.
Developing an PDF reader for Android is not really simple, so I suggest you read this PDF file online by Google Docs Reader
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://www.example.com/example.pdf";//url to pdf file
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
This may not be the thing you want, but still hope it helps you someday.
However some Android PDF APIs are http://code.google.com/p/android-pdf/source/browse/#svn/trunk/src/org/broncho/pdfreader and http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/AndroidPdfViewer/src/com/sun/
You can also look at an SO discussion on the same problem Android PDF reader from scratch
You can use one of the opensource PDF readers (eg: XPDF) and compile it for 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