在 WebView 中打开 PDF [英] Open PDF in a WebView
本文介绍了在 WebView 中打开 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的 WebView 中打开一个 PDF,我在这个论坛上找到并组合了代码.
I want to open a PDF in my WebView, and I found and combined codes on this forum.
尽管我安装了多个 PDF 应用程序,包括 Adobe Reader,但它还是会捕获未找到 PDF 应用程序".
But it catches the "No PDF application found" although I have multiple PDF apps installed, including Adobe Reader.
代码如下:
private class PsvWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (url.contains(".pdf")) {
Uri path = Uri.parse(url);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(PsvWebViewActivity.this, "No PDF application found", Toast.LENGTH_SHORT).show();
}
catch(Exception otherException)
{
Toast.makeText(PsvWebViewActivity.this, "Unknown error", Toast.LENGTH_SHORT).show();
}
}
return true;
} } }
推荐答案
(1) Google Docs Viewer,你可以在安卓浏览器中打开它,
(1) Google Docs Viewer, You can open it in android Browser like,
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+ webUrl);
更新:
(2) 在 build.gradle(app模块)添加此依赖项,
(2) Check this library, in build.gradle(app module) add this dependency,
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
这篇关于在 WebView 中打开 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文