skip to Main Content

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

2

Answers


  1. 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
            }
        }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search