简体   繁体   English

Android webview不会在按钮点击时生成pdf报告

[英]Android webview not generating pdf report on button click

I have created a web app in Android studio using WebView.我使用 WebView 在 Android Studio 中创建了一个 Web 应用程序。 I have loaded a WordPress site in android WebView.我已经在 android WebView 中加载了一个 WordPress 站点。 The WordPress site exports a pdf report on a button click When i load it on chrome browser.当我在 chrome 浏览器上加载它时,WordPress 站点通过单击按钮导出 pdf 报告。

The website loaded in chrome该网站以 chrome 加载

在此处输入图片说明

When i click on PDF button shows the window当我点击 PDF 按钮时显示窗口

在此处输入图片说明

and

在此处输入图片说明

and finally it starts downloading the pdf file最后它开始下载pdf文件

在此处输入图片说明

and the generated pdf report looks like:生成的 pdf 报告如下所示:

在此处输入图片说明

But the problem occurs when i use the Android App created in WebView.但是当我使用在 WebView 中创建的 Android 应用程序时会出现问题。 When i click on PDF button it does nothing and i get a Debug log当我点击 PDF 按钮时它什么也不做,我得到一个调试日志

D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
[InputMethodManagerWrapper.java:68] hideSoftInputFromWindow

I have manifest permission set to我已将清单权限设置为

 uses-permission android:name="android.permission.INTERNET"

Code here private static final String WEB_URL = "http://workmanager.midzonetechnologies.com/";这里的代码 private static final String WEB_URL = "http://workmanager.midzonetechnologies.com/";

webView = findViewById(R.id.webView);
    
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webView.getSettings().setBlockNetworkLoads(false);

    webView.getSettings().setAppCacheMaxSize( 10 * 1024 * 1024 ); // 10MB
    webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath() );
    webView.getSettings().setAllowFileAccess( true );
    webView.getSettings().setAppCacheEnabled( true );
    webView.getSettings().setJavaScriptEnabled( true );
    webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
    webView.setWebViewClient(new WebViewClient());
    webView.setWebChromeClient(new WebChromeClient());

    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(WEB_URL);

Please tell is the problem related to manifest file permission or run time permission, Please give a solution.请告诉是与清单文件权限或运行时权限有关的问题,请给出解决方案。

The Android app that i created in WebView.我在 WebView 中创建的 Android 应用程序。

在此处输入图片说明

What you are seeing happening in Chrome is:您在 Chrome 中看到的情况是:

  • noticing the downloadable file based on path: mywebsite.com/randomPath/randomFile**.pdf**注意基于路径的可下载文件:mywebsite.com/randomPath/randomFile**.pdf**
  • request download permissions请求下载权限
  • download the file to it's local storage将文件下载到本地存储
  • open the PDF file using Google Drive使用 Google Drive 打开 PDF 文件

If you want that behaviour you have to code all those steps in your app.如果你想要这种行为,你必须在你的应用程序中编写所有这些步骤。

A fast solution to this is to intercept the url, check if it is a pdf file and then use a browser to open it.一个快速的解决方案是拦截 url,检查它是否是 pdf 文件,然后使用浏览器打开它。

    webView.webviewClient = object : WebViewClient() {
        @TargetApi(Build.VERSION_CODES.N)
        fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {
            val url = request.url.toString()
            if (url.takeLast(4) == ".pdf") {
                startActivity(
                    Intent(
                        Intent.ACTION_VIEW,
                        "https://docs.google.com/gview?embedded=true&url=${request.url}"
                    )
                )
                return true
            }
            return false
        }
    }

You would probably have to have different steps in here.您可能必须在此处执行不同的步骤。 As @Florin T. stated, you would first have to get the Permission required for your App:正如@Florin T. 所说,您首先必须获得应用所需的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In the next step you would be detect wether the system can be downloading anything.在下一步中,您将检测系统是否可以下载任何内容。

This coud be done like so:这可以像这样完成:

mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimetype,
            long contentLength) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}});

Source here 来源在这里

If you then click on the PDF-Button in your WebView, the system should be handling the download on your phone.如果您随后单击 Web 视图中的 PDF 按钮,系统应该会处理您手机上的下载。 This means that you can then be opening your download as if it would have been downloaded via chrome.这意味着您可以像通过 chrome 下载一样打开下载。

In the last step, you would have to get the saved loaction from the download and start an Intent to open it with a local PDF-App.在最后一步中,您必须从下载中获取保存的位置并启动 Intent 以使用本地 PDF 应用程序打开它。 This could be done via android intents.这可以通过android意图来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM