我要在我的WebView打开PDF,我发现,结合codeS在这个论坛。
但它抓住我虽然安装了多个PDF的应用程序,包括未找到PDF的应用程序安装Adobe Reader。
下面的code:
私有类PsvWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
view.loadUrl(URL);
如果(url.contains(PDF)){
URI路径= Uri.parse(URL);
意图pdfIntent =新的意图(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(道路,应用程序/ PDF格式);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
尝试
{
startActivity(pdfIntent);
}
赶上(ActivityNotFoundException E)
{
Toast.makeText(PsvWebViewActivity.this,没有PDF应用程序中发现,Toast.LENGTH_SHORT).show();
}
赶上(例外otherException)
{
Toast.makeText(PsvWebViewActivity.this,未知错误,Toast.LENGTH_SHORT).show();
}
}
返回true;
}}}
解决方案
您无法打开PDF直接在android的网页浏览器,但使用谷歌文档查看器,你可以在Android浏览器中打开它就像......
mWebView.loadUrl(https://docs.google.com/gview?embedded=true&url=+ WEBURL);
I want to open a PDF in my WebView, and I found and combined codes on this forum.
But it catches the "No PDF application found" although I have multiple PDF apps installed, including Adobe Reader.
Here the code:
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;
} } }
解决方案
You cant open pdf directly in android web browser But Using google Docs Viewer you can open it in android Browser like......
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+ webUrl);