Android webview: download files like browsers do

前端 未结 5 1665
花落未央
花落未央 2020-11-29 21:33

I\'m working on an Android app with a webview pointing to a dynamic website by another team.
When i download a file (mostly dynamically redirected PDF a

5条回答
  •  鱼传尺愫
    2020-11-29 21:59

    wv.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);
            //------------------------COOKIE!!------------------------
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            //------------------------COOKIE!!------------------------
            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();
        }
    });
    

    thanks j.c for your answer you missed ); at end of code..

提交回复
热议问题