skip to Main Content

I’m using webview in an Android application. I am trying to download a .pdf file, however when the link is clicked through the application the .pdf file name is changed to "1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf.pdf", and is not saved with the original file name.

How to make webview save the file with the original name? At the moment the webview is saving the file using the ID as the name.

Used link: https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf

WebView:

webView.setDownloadListener(new DownloadListener()
        {
            @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimeType,
                                        long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(url));
                request.setMimeType(mimeType);
                String cookies = CookieManager.getInstance().getCookie(url);
                request.addRequestHeader("cookie", cookies);
                request.addRequestHeader("User-Agent", userAgent);
                request.setDescription("Downloading File...");
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(
                        Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                                url, contentDisposition, mimeType));
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
            }});

Permissions in Manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>

3

Answers


  1. An option to use DownloadManager

    For this you sould add an onNavigating event to your WebView. If the user target a pdf file you can stop the loading process with: ags.Cancel = true
    And this point when you can pass the url to a DownloadManager what will download your file.

    Login or Signup to reply.
  2. Here a simple code

    webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                if (url.contains(".pdf")){
                    downloadFile(url)
                }
                return false
            }
        }
    
        fun downloadFile(url: String) {
        Log.d(TAG, "downloadFile: url = $url")
        val manager = getSystemService(Activity.DOWNLOAD_SERVICE) as DownloadManager
        val uri =
            Uri.parse(url)
        val request = DownloadManager.Request(uri)
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        val reference: Long = manager.enqueue(request)
    }
    
    Login or Signup to reply.
  3. Here a test with your link (in Kotlin). If you need a Java example, please, let me know:

    private fun test() {
        webView = findViewById(R.id.webView)
    
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                Log.d(TAG, "shouldOverrideUrlLoading:url = ${url}")
                if (url.contains("=download")){
                    Log.d(TAG, "shouldOverrideUrlLoading: ")
                    downloadFile(url)
                    webView.stopLoading()
                }
                return true
            }
        }
        val url = "https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf"
        webView.loadUrl(url)
    }
    
    fun downloadFile(url: String) {
        Log.d(TAG, "downloadFile: url = $url")
        val manager = getSystemService(Activity.DOWNLOAD_SERVICE) as DownloadManager
        val uri =
            Uri.parse(url)
        val request = DownloadManager.Request(uri)
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        val reference: Long = manager.enqueue(request)
    }
    

    image abuot download

    Java code:

        private void test() {
    
        webView = findViewById(R.id.webView);
    
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.contains("=download")){
                    downloadFile(url);
                }
    
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    
        String url = "https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf";
        webView.loadUrl(url);
    }
    
    private void downloadFile(String url) {
        DownloadManager manager = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        manager.enqueue(request);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search