0

I have created a web app in Android studio using WebView. I have loaded a WordPress site in android WebView. The WordPress site exports a pdf report on a button click When i load it on chrome browser.

The website loaded in chrome

enter image description here

When i click on PDF button shows the window

enter image description here

and

enter image description here

and finally it starts downloading the pdf file

enter image description here

and the generated pdf report looks like:

enter image description here

But the problem occurs when i use the Android App created in WebView. When i click on PDF button it does nothing and i get a Debug log

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/";

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.

enter image description here

Ranjit Vamadevan
  • 514
  • 5
  • 21

2 Answers2

0

What you are seeing happening in Chrome is:

  • noticing the downloadable file based on path: mywebsite.com/randomPath/randomFile**.pdf**
  • request download permissions
  • download the file to it's local storage
  • open the PDF file using Google Drive

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.

    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
        }
    }
Florin T.
  • 85
  • 1
  • 9
  • That which is happening in chrome browser should happen in WebView also. I think the data is fetch from database and the pdf file is created dynamically. So we cannot use the url and let allow WebView do the rest of the thing. When i click on the PDF button soft input mode is disabled by InputMethodManagerWrapper. Please tell how to solve this. – Ranjit Vamadevan Jul 27 '21 at 11:08
  • Webview can not display pdf files. – Florin T. Jul 27 '21 at 11:14
  • why then it is not working the way it is working in chrome browser. Please please read my full question and understand it. – Ranjit Vamadevan Jul 27 '21 at 14:12
0

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:

<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. This means that you can then be opening your download as if it would have been downloaded via 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. This could be done via android intents.

Dharman
  • 30,962
  • 25
  • 85
  • 135
TIMBLOCKER
  • 328
  • 4
  • 15
  • i am getting an error..... No Activity found to handle Intent { act=android.intent.action.VIEW dat=blob:http://workmanager.midzonetechnologies.com/64c423a8-32a5-465f-a881-6d33475dbdb9 } – Ranjit Vamadevan Aug 01 '21 at 04:44
  • i have removed the "blob:" part of the url and the download still not working. – Ranjit Vamadevan Aug 01 '21 at 05:39
  • Did you include the excact download link? Is it possible that behind that link there seems to be a download but isn't? You could verify that by going to a nowmal browser and typing in that link. If it opens a "Download-Box" (normal to Windows or Mac) than the download should be okay? – TIMBLOCKER Aug 01 '21 at 09:25
  • it is downloading well in windows and android browser. The problem arising only in WebView. – Ranjit Vamadevan Aug 01 '21 at 11:33
  • Probably this thread could help you: https://stackoverflow.com/questions/10069050/download-file-inside-webview – TIMBLOCKER Aug 01 '21 at 14:12