在 WebView 中打开 PDF 文件

2024-05-11

大约 2 天,我尝试在我的自定义中打开 PDF 文件WebvView。这是我的WebView code:

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebActivity extends AppCompatActivity {
    public
    WebView mywebViewfull;
    ProgressBar progressBar;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        String url = getIntent().getStringExtra("url");
        mywebViewfull = (WebView) findViewById(R.id.webnav1);
        progressBar=(ProgressBar)findViewById(R.id.progressbar1);
        WebSettings webSettings= mywebViewfull.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setGeolocationEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSaveFormData(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setGeolocationDatabasePath(getFilesDir().getPath());
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        mywebViewfull.loadUrl(url);
        mywebViewfull.setWebViewClient(new mywebViewfullClient());


    }


    @Override
    public void onBackPressed() {
        if(mywebViewfull.canGoBack())
        {
            mywebViewfull.goBack();
        }

        else
        {
            super.onBackPressed();
        }
    }
    class mywebViewfullClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       // android.support.v7.app.ActionBar actionBar = getSupportActionBar();
       // actionBar.hide();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("http://")) {
            //open url contents in webview
            return false;
        }if (url != null && url.startsWith("https://")) {
                //open url contents in webview
            return false;
              //this must open pdf file
        }if (url.endsWith(".pdf")) {
            mywebViewfull.loadUrl("https://docs.google.com/gview?embedded=true&url="+getIntent().getStringExtra("url"));
            return false;

        }
        else {
            //external links in app
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;

        }
    }
}
    //back previous page
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction()==KeyEvent.ACTION_DOWN){
            switch (keyCode){
                case KeyEvent.KEYCODE_BACK:
                    if (mywebViewfull.canGoBack()) {
                        mywebViewfull.goBack();
                    }
                    else  {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);


    }

}

所以...当我尝试打开时,我的应用程序中没有任何反应。

我看不出出了什么问题!谁能帮我?


Android 没有原生支持打开 PDF 文件,但您有一些解决方法或多或少可以帮您完成这项工作。备择方案:

1 - 您可以打开系统默认应用程序,通过应用程序的 Intent 读取 pdf 文件;

2 - 您可以使用第三方库在应用程序中打开 pdf;

3 - 您可以调用 Google Docs 网站功能在 Web 视图中打开 pdf 文件。为此,请使用以下代码示例:

String pdfUrl = "http://yourwebsite.com/yourfile.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WebView 中打开 PDF 文件 的相关文章

随机推荐

  • 如何知道我的 Xcode iPhone 项目是否使用 ARC?

    我想知道我的 Xcode iPhone 项目是否正在使用 ARC 但我不记得在创建项目时是否勾选了该框 我怎样才能得到这些信息 选择您的项目 然后构建设置 寻找Objective C 自动引用计数 in the Apple LLVM 编译器
  • 为单个列表注册事件处理程序

    我有一个共享点事件处理程序 我想为单个列表激活它 而不是站点中的所有列表 我该怎么办 得到答案了 我们需要运行这段代码 也许在控制台应用程序中 不过 我仍然不知道如何在添加事件处理程序后将其删除 string siteUrl Console
  • 如何更改Python使用的SQLite版本?

    我在 Debian 9 12 上安装了 Python 3 8 和 SQLite 3 16 2 并且需要升级到较新版本的 SQLite 我已经下载并编译了 SQLite 网站上提供的合并 并将其放入 usr bin 所以当我这样做时 sqli
  • 通过列计数拆分时重复表头

    我正在 Magento 中输出产品列表 作为包装在表格中的简单列表 由于此列表可能会很长 100 个以上产品 因此我使用了来自这里的想法 https stackoverflow com questions 21001803 how to h
  • Solr PatternReplaceCharFilterFactory 未替换为指定模式

    所以我对 Solr 很陌生 但我尝试使用 PatternReplaceCharFilterFactory 对将存储的电话号码字符串进行一些预处理 这是该字段的配置
  • 在 xcode 中,有没有办法验证所有 NSLocalizedStrings 的密钥?

    除了运行其中包含 NSLocalizedString 的每个代码路径之外 是否有一种方法可以验证所有 NSLocalizedString 是否都具有实际存在于所有捆绑包的所有 Localized strings 文件中的密钥 例如 一个键中
  • FluentAssertions ShouldNotThrow 无法识别异步方法/Func

    我正在尝试检查异步方法是否抛出具体异常 为此 我使用 MSTEST 和 FluentAssertions 2 0 1 我已经检查过这个关于 Codeplex 的讨论 http fluentassertions codeplex com wo
  • 在 try 中使用零合并运算符? for 抛出并返回可选值的函数

    我想在以下两种情况下使用 nil coalescing 运算符设置默认值 函数抛出错误 函数返回 nil 请看一下下面的代码片段 我有以下问题 为什么 item1 为零 item1和item2的初始化有什么区别 enum VendingMa
  • 自定义 php 论坛 - 显示新的/未读的帖子

    我自己使用 php 编写了一个自定义论坛脚本 我决定不使用 phpbb 和其他工具 因为我希望我所做的事情具有 100 的灵活性 不过我遇到了一个问题 如何向用户显示帖子是否是新的 未读的 我想到了两种解决方案 1 饼干 2 数据库 我不想
  • mocha——手表和猫鼬模型

    如果我让 mocha 监视更改 每次保存文件时 mongoose 都会抛出以下错误 OverwriteModelError 无法覆盖Client模型一旦编译 我知道猫鼬不允许两次定义模型 但我不知道如何让它与mocha watch clie
  • 如何调试.NET Windows Service OnStart方法?

    我用 NET 编写的代码仅在作为 Windows 服务安装时才会失败 该故障甚至不允许服务启动 我不知道如何进入 OnStart 方法 如何 调试 Windows 服务应用程序 http msdn microsoft com en us l
  • 如何在查询中生成序列号?

    我们使用 PostgreSQL v8 2 3 如何在查询输出中生成序列号 我想显示查询返回的每一行的序列号 例子 SELECT employeeid name FROM employee 我希望生成并显示从一开始的每一行的序列号 你有两个选
  • 如何使用 javascript 禁用组合键?

    I would like to disable view source shortcut key for IE using JavaScript To disable Ctrl C I am using the following func
  • 超慢的表格布局性能

    我遇到了糟糕的 TableLayout 性能 我在这里读过一些帖子 谈论同样的事情 Android 动态创建表 性能不佳 https stackoverflow com questions 9813427 android dynamical
  • pdflatex: \includegraphics{} -> 找不到文件

    首先 我知道这个问题已经存在了成百上千次 但我在过去四个小时内找到的给出的答案都没有解决我的具体问题 我在这里变得疯狂 我将非常感谢任何帮助和建议 尝试编译一个非常简单的 tex 文件 其中包括 包括图形命令 我最终收到 文件未找到 错误
  • AS3 [Event(name="", type="")],有什么意义?

    我使用 FlashDevelop3 R2 和 Flex 3 3 SDK 进行开发 在很多情况下我必须使用嵌入元数据标签 如下所示 Embed source path to file private var Asset Class 我很好地理
  • Spring Oauth2. DaoAuthenticationProvider 中未设置密码编码器

    我对 Spring Oauth 和 Spring Security 很陌生 我正在尝试在我的项目中使用 client credentials 流程 现在 我设法使用自己的 CustomDetailsS ervice 来从系统中已存在的数据库
  • 通过API更新Twitter背景

    我在通过 Twitter 的 API 更新背景时遇到了一些问题 target url http www google com logos 11th birthday gif ch curl init curl setopt ch CURLO
  • 用于验证 InetSocketAddresses 的正则表达式(ipv4/v6 + 端口地址)

    我在寻找testedipv4 和 ipv6 的正则表达式InetSocket地址 http download oracle com javase 6 docs api java net InetSocketAddress html toSt
  • 在 WebView 中打开 PDF 文件

    大约 2 天 我尝试在我的自定义中打开 PDF 文件WebvView 这是我的WebView code import android app AlertDialog import android app ProgressDialog imp