首页 > 解决方案 > 无法在 Web 视图中加载 PDF 文件

问题描述

我有个问题。我开发了一个带有 webview 组件的项目,但我无法在 webview 中显示 pdf 文件,因为在网站链接中,所有使用新选项卡打开的 pdf 文件和 pdf 文件都是用户会话的身份验证。我在官方 Android Chrome 应用程序上测试了这个网站链接,所有 pdf 文件都打开并自动下载了移动存储中的 PDF 文件,但在 webview 组件上我无法显示这个。请你帮我解决这个话题。提前致谢。我的代码是:

public class MainActivity extends AppCompatActivity {
private WebView webView = null;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.webView = (WebView) findViewById(R.id.webview);
    CookieManager.getInstance().setAcceptCookie(true);
    this.button = (Button) findViewById(R.id.button);

    webView.addJavascriptInterface(new WebAppInterface(this), "Android");
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setDomStorageEnabled(true);
   String DESKTOP_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";
    webView.getSettings().setUserAgentString(DESKTOP_USER_AGENT);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @SuppressLint("InlinedApi")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            CookieSyncManager.getInstance().sync();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                CookieManager cookieManager = CookieManager.getInstance();

                cookieManager.setAcceptThirdPartyCookies(webView, true);

            } 

                Intent intent = new Intent(Intent.ACTION_VIEW);
                Uri videoUri = Uri.parse(url);
                intent.setDataAndType( videoUri, "application/pdf" );
                intent.setPackage( "com.example.webview" );
                startActivity( intent );

            view.loadUrl(url);
                return true; // to tell the WebView that it should not load the URL because you handled it by yourself

        }
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed(); // Ignore SSL certificate errors
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { 
            showDefaultError();
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            CookieSyncManager.getInstance().sync();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                CookieManager cookieManager = CookieManager.getInstance();

                cookieManager.setAcceptThirdPartyCookies(webView, true);

            } 
            super.onPageFinished(view, url);

        }
    });

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {

        //put your code here if your want to show the progress with progressbar
    }
});

webView.loadUrl(“MY LINK”);

标签: javaandroidpdf

解决方案


推荐阅读