Android Web View下载的文件“无法打开文件”消息

时间:2017-04-21 13:11:54

标签: android webview android-download-manager

我有以下代码用于处理Webview中的下载,但是当我尝试从下载中打开文件时,它说无法打开文件。如果我转到文件管理器应用程序或相应的应用程序打开下载的文件,它会打开而不会出现问题。任何人都可以了解正在发生的事情以及我如何解决这个问题。

asw_view.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("application/jpeg");
            //------------------------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();
        }
    });

1 个答案:

答案 0 :(得分:0)

我认为问题是模仿类型,请检查此链接https://www.sitepoint.com/mime-types-complete-list。第二,您对请求request.setMimeType("application/jpeg")request.setMimeType(mimeType)request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));设置模仿类型的两种不同方式

相关问题