我正在使用 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”的文件。我希望它以默认名称保存文件
最佳答案
您应该能够从 url 中获取文件名,如下所示:
String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
然后只需在这一行中传递它,而不是硬编码字符串“filename
”:
myRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
关于java - WebView下载管理器: How to download files with their default names?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709432/