gpt4 book ai didi

android - 如何在 webview android 6 中打开 pdf

转载 作者:行者123 更新时间:2023-11-29 01:17:05 25 4
gpt4 key购买 nike

我设法在 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);

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

     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 上的黑屏!任何人都可以帮助我吗?谢谢

最佳答案

试试这个:

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();
}
}

关于android - 如何在 webview android 6 中打开 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com