如何在Android的WebView中打开本地PDF文件? [英] How to open local PDF file in WebView in Android?

查看:219
本文介绍了如何在Android的WebView中打开本地PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WebView 中打开本地(SD 卡)PDF 文件.

我已经试过了:

webview = (WebView) findViewById(R.id.webview);webview.getSettings().setJavaScriptEnabled(true);webview.getSettings().setPluginsEnabled(true);webview.getSettings().setAllowFileAccess(true);File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");最终的 Uri uri = Uri.fromFile(file);webview.loadUrl(uri.toString());

但它仍然无法打开,所以请告诉我如何在 WebView 中打开 PDF?

解决方案

我知道,这个问题很老了.

但我真的很喜欢 Xamarin 使用 Mozilla 的 pdf.js 的方法.它适用于较旧的 Android 版本,您不需要为此使用特殊的 PDF 查看器应用程序,您可以轻松地在应用程序视图层次结构显示 PDF.

Git:

并按以下方式调用它:

//假设您获得了 pdf 文件:File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");webview = (WebView) findViewById(R.id.webview);WebSettings 设置 = webview.getSettings();settings.setJavaScriptEnabled(true);settings.setAllowFileAccessFromFileURLs(true);settings.setAllowUniversalAccessFromFileURLs(true);settings.setBuiltInZoomControls(true);webview.setWebChromeClient(new WebChromeClient());webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + file.getAbsolutePath() + "#zoom=page-width");

很酷的事情:如果你想减少功能/控件的数量.转到 Assets/pdfjs/web/viewer.html 文件并将某些控件标记为隐藏.随着

style="display: none;"

例如如果您不喜欢正确的工具栏:

<div id="toolbarViewerRight" style="display: none;">...</div>

更新

<块引用>

'不支持 URL 方案文件"'

较新版本的 pdfjs 可能会发生.使用版本 1.8.188 不会出现此错误.

I want to open local (SD card) PDF file in a WebView.

I already tried this:

webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setAllowFileAccess(true);
File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");

final Uri uri = Uri.fromFile(file);

webview.loadUrl(uri.toString());

But it's still not opening it, so let me know how I can open a PDF in WebView?

解决方案

I know, this question is old.

But I really like the approach of Xamarin to make use of the pdf.js from Mozilla. It works on older Android versions, you don't need a special PDF Viewer app for this and you can easily display a PDF inside of your apps views hierarchy.

Git for this: https://mozilla.github.io/pdf.js/

Additional default options (like standard zoom): https://github.com/mozilla/pdf.js/wiki/Viewer-options

Just add the pdfjs files to your Assets directory:

And call it the following way:

// Assuming you got your pdf file:
File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");

webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + file.getAbsolutePath() + "#zoom=page-width");

Cool thing: If you want to reduce the amount of functionalities / controls. Go to the Assets/pdfjs/web/viewer.html file and mark certain controls as hidden. With

style="display: none;"

E.g. If you don't like the right toolbar:

<div id="toolbarViewerRight" style="display: none;">...</div>

Update

'URL scheme "file" is not supported'

Might occur for newer versions of pdfjs. With version 1.8.188 this error does not appear.

这篇关于如何在Android的WebView中打开本地PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆