我有以下代码用于处理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();
}
});
答案 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));
设置模仿类型的两种不同方式>