我想从Android的WebView下载文件,但是遇到了麻烦。 如果要下载的文件是仅在登录站点后才能下载的文件,我应该如何编写该过程? (如果您直接进入URL,则在无法访问时会显示该信息)
目前我正在编写这样的过程。
■情况
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
String filename = "test";
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, filename);
DownloadManager dm = (DownloadManager) getSystemService(android.content.Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
在这样的情况下,如果您能告诉我是否有原因,那将非常有帮助