AsyncTask to manually download the file name:
private static class getFileNameAsync extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... params) { URL url; String filename = null; HttpURLConnection conn = null; try { url = new URL(params[0]); conn = (HttpURLConnection) url.openConnection(); conn.connect(); conn.setInstanceFollowRedirects(false); try { for(int i = 0; i < 100; i++) { String stringURL = conn.getHeaderField("Location"); if (stringURL != null) { url = new URL(stringURL); conn = (HttpURLConnection) url.openConnection(); conn.connect(); conn.setInstanceFollowRedirects(false); } else { i = 100; } } } catch (Exception e) { e.printStackTrace(); } String depo = conn.getHeaderField("Content-Disposition"); if (depo != null) { String depoSplit[] = depo.split(";"); int size = depoSplit.length; for(int i = 0; i < size; i++) { if(depoSplit[i].startsWith("filename=")) { filename = depoSplit[i].replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1").trim(); i = size; } } } } catch (MalformedURLException e){ e.printStackTrace(); } catch (ProtocolException e){ e.printStackTrace(); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } finally { if (conn != null) conn.disconnect(); } return filename; } @Override protected void onPostExecute(String filename) { super.onPostExecute(filename); } }
DownloadListener:
mWebView.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { if(Build.VERSION.SDK_INT >=23){ Context nContext = MainActivity.this.getApplicationContext(); if(nContext.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ MainActivity.this.requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); return; } } String fileName = ""; url = url.replace(" ", "%20"); AsyncTask<String, Void, String> asyncTask = new getFileNameAsync(); asyncTask.execute(url); try { fileName = asyncTask.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (CancellationException e) { e.printStackTrace(); } if ((fileName == null) || (fileName.hashCode() == "".hashCode())) { fileName = URLUtil.guessFileName(url, contentDisposition, mimetype); } 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(fileName); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); if (downloadManager != null) { downloadManager.enqueue(request); } Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show(); } });
Alexander Savin
source share