I want to download some files using my own webview, but when i use download manager i am not able to get the default name of the file before downloading it.
I tried this but it does not work
String fileName = url.substring(url.lastIndexOf('/') + 1);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName );
Any ideas?
What you need:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url2) {
if(url2.startsWith("http://www.youtube-mp3.org/get?ab=")){
if(isDownloadManagerAvailable(MainActivity.this)){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url2));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setMimeType("audio/MP3");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
//Get file name as string
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, -----);//put it here
manager.enqueue(request);
}
}
else{
view.loadUrl(url2);
}
return false;
}