如何显示包含 pdf 的 url 并将其显示在 Webview Android Java 中

Posted

技术标签:

【中文标题】如何显示包含 pdf 的 url 并将其显示在 Webview Android Java 中【英文标题】:How to show url that contains a pdf and show it embedded in a Webview Android Java 【发布时间】:2018-07-25 17:24:57 【问题描述】:

我正在尝试显示一个包含 pdf 的网址。我想显示它嵌入在 webview 中,但它正在生成错误并且我找不到解决方案,我不知道它是否来自 google 查看器。请,如果有人可以帮助我,我将不胜感激。

public class PoliticaTratamientoDatos extends AppCompatActivity
    private ImageView imgBotonVolver;
    private WebView webViewPolitica;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_politica_tratamiento_datos);
        imgBotonVolver = (ImageView) findViewById(R.id.imgBotonVolver);
        imgBotonVolver.setOnClickListener(this);

        webViewPolitica = (WebView) findViewById(R.id.webViewPolitica);
        webViewPolitica.getSettings().setjavascriptEnabled(true);


        String pdf = "https://www.bancoagrario.gov.co/canales/Seguridad/Documents/DocTratamientoDatosPersonales.pdf";
        webViewPolitica.loadUrl("https://docs.google.com/viewer?url=" + pdf);
        //webViewPolitica.loadUrl("https://docs.google.com/viewer?url=" + pdf + "&embedded=true");
    

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:orientation="vertical">
    <LinearLayout
        android:layout_marginTop="10sp"
        android:layout_marginBottom="10sp"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_
        android:layout_>
        <WebView
            android:id="@+id/webViewPolitica"
            android:layout_
            android:layout_>
        </WebView>
    </LinearLayout>
</LinearLayout>

我的网页视图中的错误是:

<html><head><meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="-1"/>
<meta http-equiv="CacheControl" content="no-cache"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="shortcut icon" href="data:;base64,iVBORw0KGgo="/>

<script>

(function()
    var securemsg;
    var dosl7_common;

window["bobcmn"] = "1111101010101020000000220000000520000000022a40ac4f200000096300000000300000000300000006/TSPD/300000008TSPD_101300000005https200000000200000000";

window.RaP=!!window.RaP;try(function()tryvar ll,Ll,Ol=1,Sl=1,Il=1;for(var Jl=0;Jl<Ll;++Jl)Ol+=2,Sl+=2,Il+=3;ll=Ol+Sl+Il;window.z_===ll&&(window.z_=++ll)catch(lL)window.z_=llvar zL=!0;function ZL(l)l&&(zL=!1,document.cookie="brav=ad");return zLfunction iL()ZL(window[iL.name]===iL);ZL("function"!==typeof ie9rgb4);ZL(/\x3c/.test(function()return"\x3c")&!/x3d/.test(function()return"'x3'+'d';"));
var jL=window.attachEvent||/mobi/i.test(window["\x6e\x61vi\x67a\x74\x6f\x72"]["\x75\x73e\x72A\x67\x65\x6et"]),JL=+new Date+6E5,_o,Io,Jo=setTimeout,lO=jL?3E4:6E3;function LO()if(!document.querySelector)return!0;var l=+new Date,z=l>JL;if(z)return ZL(!1);z=Io&&_o+lO<l;z=ZL(z);_o=l;Io||(Io=!0,Jo(function()Io=!1,1));return zLO();var OO=[17795081,27611931586,1558153217];
function zO(l)l="string"===typeof l?l:l.toString(36);var z=window[l];if(!z.toString)return;var S=""+z;window[l]=function(l,S)Io=!1;return z(l,S);window[l].toString=function()return Sfor(var sO=0;sO<OO.length;++sO)zO(OO[sO]);ZL(!1!==window.RaP);
(function SO()if(!LO())return;var z=!1;function S(z)for(var s=0;z--;)s+=_(document.documentElement,null);return sfunction _(z,s)var S="vi";s=s||new I;return _l(z,function(z)z.setAttribute("data-"+S,s.ol());return _(z,s),null)function I()this._j=1;this.sj=0;this.zS=this._j;this.c=null;this.ol=function()this.c=this.sj+this.zS;if(!isFinite(this.c))return this.reset(),this.ol();this.sj=this.zS;this.zS=this.c;this.c=null;return this.zS;this.reset=function()this._j++;this.sj=0;this.zS=this._j
var J=!1;function s(z,s)if(!LO())return;var S=document.createElement(z);s=s||document.body;s.appendChild(S);S&&S.style&&(S.style.display="none");LO()function zl(s,S)if(!LO())return;S=S||s;var _="|";function I(z)z=z.split(_);var s=[];for(var S=0;S<z.length;++S)var J="",ol=z[S].split(",");for(var zl=0;zl<ol.length;++zl)J+=ol[zl][zl];s.push(J)return svar zl=0,_l="datalist,details,embed,figure,hrimg,strong,article,formaddress|audio,blockquote,area,source,input|canvas,form,link,tbase,option,details,article";
_l.split(_);_l=I(_l);_l=new RegExp(_l.join(_),"g");while(_l.exec(s))_l=new RegExp((""+new Date)[8],"g"),z&&(J=LO()),++zl;return LO()?S(zl&&1):void 0function _l(z,S,_)if(!LO())return;(_=_||J)&&s("div",z);z=z.children;var I=0;for(var zl in z)_=z[zl];try_ instanceof HTMLElement&&(S(_),++I)catch(_l)return LO()?I:void 0zl(SO,S);LO())();var _O=95;window.ij=Jj:"081b250c89017800dc2d9278ef592d39fb30f82160d2a2a82186935b699c223b6e87475b486458e48c010d7b7b4ad500aa1e0c7c1d561dc7fb109bde286064f19d9e584f8837a118e94e111b8d72348c7c116746bbd2a93704e9a956b7ea023075ba10d3e0516cc2be6d3cc8f122314e647d2cc4907d35109c3cb105c6360463";function L(l)return 947>l
function O(l)var z=arguments.length,S=[];for(var _=1;_<z;++_)S.push(arguments[_]-l);return String.fromCharCode.apply(String,S)function Z(l,z)l+=z;return l.toString(36)(function iO(z)return z?0:iO(z)*iO(z))(LO());)();catch(x)document.cookie='brav=oex'+x;finallyie9rgb4=void(0);;function ie9rgb4(a,b)return a>>b>>0;

)();

</script>

<script type="text/javascript" src="/TSPD/08d7da57a9ab2000f6d4855c247e06d6be4b102a7e42062d10476ec486a8a0201e92a422e6e2460b?type=7"></script>
<noscript>Please enable JavaScript to view the page content.</noscript>
</head><body>
</body></html>

我不知道我做错了什么,请帮忙!

【问题讨论】:

【参考方案1】:

您的 .pdf 来自 Google 文档,Google 正在将您重定向到另一个 URL。

使用下面的代码作为WebView

webViewPolitica.setWebViewClient(new WebViewClient() 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
        view.loadUrl(url);
        return false;
    
);

编辑:

link 有时在我的浏览器中不起作用,所以我认为 .pdf 链接会导致问题。有时mybrowser在打开it时会出现ERR_CONNECTION_REFUSED。

希望对您有所帮助。

【讨论】:

您检查过我编辑的答案吗?好像是URL连接问题 我建议你下载pdf文件并从本地资源加载它

以上是关于如何显示包含 pdf 的 url 并将其显示在 Webview Android Java 中的主要内容,如果未能解决你的问题,请参考以下文章

从数据库中提取 pdf 并将其显示在我的 cshtml 上

如何在模态窗口中显示 pdf? [关闭]

从 API URL 下载 PDF

如何从 jQuery 发送数组并将其显示在 grails 控制器中?

如何删除 URL 中的参数并将其显示在地址栏中而不会导致 Javascript 中的重定向?

如何使用CakePdf创建和下载PDF,而不是在浏览器中显示它