wv.setDownloadListener(new DownloadListener()
{
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
{
try {
contentDisposition = URLDecoder.decode(contentDisposition,"UTF-8"); //디코딩
String FileName = contentDisposition.replace("attachment; filename=", "");
String fileName = FileName.substring(1, FileName.length()-1);//양쪽에 따옴표가 있어서 제거해줌
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.setTitle(fileName);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),getString(R.string.file_downloading), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(getBaseContext(), getString(R.string.file_permission), Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1004);
}
else {
Toast.makeText(getBaseContext(), getString(R.string.file_permission), Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1004);
}
}
}
}
});