[英]WebView Download Manager: How to download files with their default names?
I'm developing a simple webview app using Android Studio. 我正在使用Android Studio开发一个简单的Webview应用程序。 The webview app is booting a website that contains downloadable media files.
该webview应用程序正在引导一个包含可下载媒体文件的网站。 For now, after downloading a file it save the file as fileName.
现在,下载文件后,将文件另存为fileName。 This is my Download manager
这是我的下载经理
String myCurrentUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
superWebView = findViewById(R.id.myWebView);
superProgressBar.setMax(100);
superWebView.loadUrl("http://www.hausadownload.blogspot.com");
superWebView.getSettings().setJavaScriptEnabled(true);
superWebView.setWebViewClient(new WebViewClient(){});
superWebView.setWebChromeClient(new WebChromeClient());
superWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request myRequest = new DownloadManager.Request(Uri.parse(url));
myRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName");
myRequest.allowScanningByMediaScanner();
myRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager myManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
myManager.enqueue(myRequest);
Toast.makeText(MainActivity.this, "Your file is downloading...", Toast.LENGTH_SHORT).show();
}
});
}
I don't want it to be saving all files with the name "fileName". 我不希望它保存名称为“ fileName”的所有文件。 I want it to be saving files with their default names
我希望它使用默认名称保存文件
You should somehow be able to get the file name from the url like so: 您应该能够以某种方式从url获取文件名,如下所示:
String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
Then just pass that in this line instead of hardcoded String " filename
": 然后只需在该行中传递它,而不是硬编码的字符串“
filename
”:
myRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.