【问题标题】:Android Web View Downloaded File "Can't Open File" MessageAndroid Web 查看下载的文件“无法打开文件”消息
【发布时间】:2017-09-18 12:17:18
【问题描述】:

我有以下代码用于处理 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();
        }
    });

【问题讨论】:

  • 你能解决这个问题吗?我遇到了同样的问题

标签: android webview android-download-manager


【解决方案1】:

我认为问题出在 mimetype 上,请查看此链接 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)); 上以两种不同的方式设置 mimetype

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多