Я могу загрузить PDF-файлы меньшего размера в WebView. Но файлы большого размера выдают ошибку Invalid Format. Более того, размер загружаемого файла составляет 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();
}
}
});