在离线时在webview中加载pdf

时间:2014-05-09 04:36:51

标签: android pdf webview

如何将PDF嵌入HTML并在Web视图中显示? 实际上我正在尝试这个,但webview没有加载这个页面。

    String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
            String pdfPath = base + "/Principal.pdf";
             Log.e("real path =", ""+pdfPath);
            web.getSettings().setJavaScriptEnabled(true);
            web.getSettings().setAllowFileAccess(true);
            web.getSettings().setBuiltInZoomControls(true);
            web.loadDataWithBaseURL("", 
                    "<embed src='"+pdfPath+"' width='500' height='375'>",
                    "application/pdf",
                    "utf-8",
                    "");

1 个答案:

答案 0 :(得分:0)

愿它有用:

对于在线:

String myPdfUrl = "http://example.com/awesome.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
Log.i(TAG, "Opening PDF: " + url);
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url);

对于OffLine:

try
{
 Intent intentUrl = new Intent(Intent.ACTION_VIEW);
 intentUrl.setDataAndType(url, "application/pdf");
 intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 myActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
 Toast.makeText(myActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}