I have generated a pdf using itext library and it gets stored in sdcard. I have to open the pdf in webview , but when I searched for the solution I came to know I can open it using online google doc service ,but my pdf is stored in a sdcard. Is there a way to open a pdf from external storage in a webview?
Asked
Active
Viewed 2.3k times
3
-
No men it's not possible in Android. but you can used some of Third Party Library fro opening pdf within your app. – M D May 20 '14 at 05:34
-
Why do you need to open it in a webview? Have you looked here?: http://stackoverflow.com/questions/2456344 – Steven Trigg May 20 '14 at 05:39
-
no webView not embed pdf it supports only html/text but you can read by using default intent.if you want code plz let me know. – Rohit Goswami May 20 '14 at 05:43
-
@RohitGoswami I have the code, I was just to know if i can open it in a webview or not. – user2699728 May 20 '14 at 05:50
-
I don't think so.i have tried this before but not working at all http://stackoverflow.com/questions/23556693/load-pdf-in-webview-while-offline – Rohit Goswami May 20 '14 at 05:52
-
@RohitGoswami So how have you managed it? – user2699728 May 20 '14 at 05:53
-
Nope but R&D goin on... – Rohit Goswami May 20 '14 at 05:55
-
@RohitGoswami will together do R&D and whosoever gets the ans will update it to other...!! :P – user2699728 May 20 '14 at 06:01
2 Answers
3
To open pdf in Webview , it better to show pdf via google doc service,
WebView webView = (WebView) context.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf");
this may help you

Akash Moradiya
- 3,318
- 1
- 14
- 19
-
5I'm not a huge fan of this solution because it opens the browser app which isn't really what I want to do if I'm embedding a webview within the app. – CodyEngel May 14 '15 at 20:32
-
You may need to override the WebViewClient for the WebView in order to make it not open the browser (redirects might be the cause). See this: http://stackoverflow.com/a/18921867/2353588 – wblaschko Nov 15 '16 at 21:34
-
-
-
-1
No, you can`t use WebView to show a PDF file from a sdcard. Only HTML file maybe shown.
The most simple solution of this task is to use an external PDF reader. Such approach it is convenient for users enough because a "hand made" PDF reader you create from a github open source code can be slow for displaying large pdf files.
public class PDFReader {
public void read(Activity context, String fileName){
File appFolder = new File( Environment.getExternalStorageDirectory(),
context.getBaseContext().getPackageName() );
File file = new File(appFolder, fileName);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
context.startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(context, "На устройстве не найдено доступного приложения для отображения PDF!",
Toast.LENGTH_SHORT).show();
}
}
}

DenisMath
- 547
- 4
- 8