Android Web视图不显示PDF

时间:2019-07-17 10:40:49

标签: android pdf webview

我有一个在浏览器中显示PDF的URL,我创建了一个WebView,但没有显示pdf。

我已经将WebViewWebViewClient的所有设置都提供了,但是ProgressBar才开始,并且pdf尚未加载。

class KnowledgeShareWebview : AppCompatActivity() {

    lateinit var webView: WebView
    lateinit var progressBar: ProgressBar
    var url = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_knowledge_share_webview)
        webView = findViewById(R.id.webView)
        progressBar = findViewById(R.id.progressBar)

        if (intent != null) {
            url = intent.getStringExtra("url")
        }

        Helper.printMessege(KnowledgeShareWebview::class.java.simpleName, "url....$url")
        initWebView()
        webView.loadUrl(url)

    }

    private fun initWebView() {
        webView.settings.loadWithOverviewMode = true
        webView.settings.useWideViewPort = true
        webView.clearCache(true)
        webView.clearHistory()
        webView.settings.javaScriptEnabled = true
        webView.settings.useWideViewPort = true
        webView.isHorizontalScrollBarEnabled = false
        webView.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
                super.onPageStarted(view, url, favicon)
                progressBar.visibility = View.VISIBLE
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                webView.loadUrl(url)
                return super.shouldOverrideUrlLoading(view, url)
            }

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                progressBar.visibility = View.GONE
                invalidateOptionsMenu()
            }

            override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
                super.onReceivedError(view, request, error)
                progressBar.visibility = View.GONE
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须调用webview.load(url)。

webView.Settings.JavaScriptEnabled = true;
        webView.Settings.SupportZoom();
        webView.Settings.SetAppCacheEnabled(true);
        webView.Settings.DomStorageEnabled = true;
        webView.ZoomOut();
        webView.ZoomIn();
        webView.Settings.BuiltInZoomControls = true;
        webView.Settings.LoadWithOverviewMode = true;
        webView.Settings.UseWideViewPort = true;
        //webview.Settings.SetSupportZoom (true);
        webView.Settings.SetPluginState(WebSettings.PluginState.On);
        webView.Settings.GetPluginState();
        webView.LoadUrl(externalurl);
        //after loading url call the webviewclient
        webView.SetWebViewClient(new MonkeyWebViewClient(imgViewBack, imgViewForward, imgRefresh));
        webView.SetWebChromeClient(new WebChromeClient());

答案 1 :(得分:0)

Android可以在WebView中显示PDF文档。但是,WebView不包含允许您显示PDF文档的PDF插件。一种解决方案是使用Intent对象启动可以处理PDF文档的第三方应用程序(例如Adobe Acrobat)。但是,这会将控制权转移到thrid-party应用。

如果您坚持使用WebView显示PDF文档,则可以使用以下技巧。您可以使用Google文档打开PDF文档,然后使用WebView加载Google文档的URL。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    WebView webView=new WebView(MainActivity.this);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(PluginState.ON);

    //---you need this to prevent the webview from
    // launching another browser when a url
    // redirection occurs---
    webView.setWebViewClient(new Callback());

    String pdfURL = "url";
   webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);

    setContentView(webView);
}

private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(
            WebView view, String url) {
        return(false);
    }
}