我正在使用Android Studio开发一个简单的Webview应用程序。该webview应用程序正在引导一个包含可下载媒体文件的网站。现在,下载文件后,将文件另存为fileName。 这是我的下载经理
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();
}
});
}
我不希望它保存名称为“ fileName”的所有文件。我希望它使用默认名称保存文件
答案 0 :(得分:0)
您应该能够以某种方式从网址中获取文件名:
String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
然后只需在该行中传递它,而不是硬编码的字符串“ filename
”:
myRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);