webview.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
String[] fnm = url.split("/");
String fname = fnm[fnm.length - 1]; // 파일이름
String fhost = fnm[2]; // 도메인
DownloadManager mDownloadManager = (DownloadManager) getBaseContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request mRequest = new DownloadManager.Request(uri);
// mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); //NETWORK_MOBILE
// mRequest.setAllowedOverRoaming(false);
mRequest.setTitle(fname);
mRequest.setDescription(fhost);
mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fname);
File pathExternalPublicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
pathExternalPublicDir.mkdirs();
// mRequest.setMimeType(HTTP.OCTET_STREAM_TYPE);
mRequest.setShowRunningNotification(true);
mRequest.setVisibleInDownloadsUi(true);
long downloadId = mDownloadManager.enqueue(mRequest);
Toast.makeText(getBaseContext(), "다운로드가 시작되었습니다.", Toast.LENGTH_SHORT).show();
// 또는 확장자를 판단하여 연계할 앱으로 보내줄 수 있다.
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setType(mimetype);
// intent.setData(Uri.parse(url));
// mContext.startActivity(intent);
}
});
다운로드 받을 파일이 아니라 html이 다운로드가 되버리는데 왜 그러는걸까요 ㅠㅠ..