如何在webview android 6中打开pdf

时间:2016-08-10 13:47:47

标签: android android-studio android-webview

我设法在android 4和5中的webview中打开一个pdf链接但是在android 6中相同的代码没有打开,这里是我的代码

    webPDF = (WebView)findViewById(R.id.webPDF);

    String auxPDF = "http://example link to pdf.pdf";
    webPDF.getSettings().setJavaScriptEnabled(true);
    webPDF.getSettings().setLoadWithOverviewMode(true);
    webPDF.getSettings().setDisplayZoomControls(true);
    webPDF.loadUrl("https://docs.google.com/gview?embedded=true&url="+ auxPDF);

我也尝试这个其他代码

webPDF.getSettings().setUseWideViewPort(true);
webPDF.getSettings().setDomStorageEnabled(true);

这里是在主要活动中打开的权限:

     if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.INTERNET}, 123);
    }
    if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_NETWORK_STATE)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.ACCESS_NETWORK_STATE}, 123);
    }
    if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
    }
    if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 123);
    }

唯一显示的是android 6.0.1上的空白屏幕! 任何人都可以帮助我吗?感谢

1 个答案:

答案 0 :(得分:1)

试试这个:

urlWebView.setWebViewClient(new AppWebViewClients());
urlWebView.getSettings().setJavaScriptEnabled(true);
urlWebView.getSettings().setLoadWithOverviewMode(true);
urlWebView.setHorizontalScrollBarEnabled(false);
urlWebView.getSettings().setUseWideViewPort(true);
urlWebView.getSettings().setBuiltInZoomControls(true);

 private class AppWebViewClients extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

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

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