I am using webview for showing my website to my users. The user needs to login to access files from my websites. Most of the files are in pdf format. But I noticed that the files not downloading properly and the files downloaded are just in html format and it only contain a error 44 error Page not found. After I googled about this problem I found that it may happening because the app doesn't in login state while downloading. I am new to android development so I request for a clear answer if possible for my understanding. Please check my code
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength){
checkDownloadPermission();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Download file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
try {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, new URL(url).getFile());
} catch (MalformedURLException e) {
e.printStackTrace();
}
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
});
// This will handle downloading. It requires Gingerbread, though
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
// This is where downloaded files will be written, using the package name isn't required
// but it's a good way to communicate who owns the directory
final File destinationDir = new File (Environment.getExternalStorageDirectory(), getPackageName());
if (!destinationDir.exists()) {
destinationDir.mkdir(); // Don't forget to make the directory if it's not there
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.getSettings().setDomStorageEnabled(true);
CookieManager.getInstance().acceptCookie();
CookieManager.setAcceptFileSchemeCookies(true);
webView.getSettings().setJavaScriptEnabled(true);
boolean shouldOverride = false;
// We only want to handle requests for mp3 files, everything else the webview
// can handle normally
if (url.endsWith(".pdf")) {
shouldOverride = true;
Uri source = Uri.parse(url);
// Make a new request pointing to the mp3 url
DownloadManager.Request request = new DownloadManager.Request(source);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
// Use the same file name for the destination
File destinationFile = new File (destinationDir, source.getLastPathSegment());
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
manager.enqueue(request);
}
else
view.loadUrl(url);
return shouldOverride;
}