[英]Why larger pdf files result in invalid format that are downloaded from android WebViewClient?
我可以在 WebView 中下载更小的 pdf 文件。 但大尺寸文件会报错 Invalid Format.Moreover 下载的文件大小为 2B。 任何帮助,将不胜感激。
mWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_READ_PHONE_STATE);
Toast.makeText(getApplicationContext(), "Please retry.",
Toast.LENGTH_LONG).show();
} else {
String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.setDescription("File Download");
request.addRequestHeader("User-Agent", userAgent);
request.setTitle(filename);
request.setMimeType(mimetype);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
spinner.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Downloading File...",
Toast.LENGTH_LONG).show();
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.