java - 无法在 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”);
解决方案
推荐阅读
- python - Django - Teplatetag "readmore" 想要成为 "readless"
- vb.net - 将此代码从 vb6 转换为 vb.net BigEndian 到整数
- c# - 动态扩展 XAML 窗口
- reactjs - 反应 Highcharts 错误
- javascript - UIkit 3 的过滤器组件:使用 URL 哈希设置活动过滤器 - 无法使用 JS 更改活动过滤器
- .net - 如何在 vb.net 中将列添加到数据库后更新 DataGridView
- node.js - 在 Node.js 中根据部署平台(dev、staging、qa、production)设置 Config 属性
- json - Clickhouse/Kafka:将 JSON 对象类型读入字段
- html - HTML/CSS 导航位置
- typescript2.0 - Typescript-过滤器:动态地在过滤器中提供多个条件