gpt4 book ai didi

android - 下载时我没有得到默认文件名

转载 作者:行者123 更新时间:2023-11-29 17:50:22 24 4
gpt4 key购买 nike

我创建了一个 webview 应用程序,我在其中添加了下载器,下载以 m4a 结尾的文件。应用程序下载了文件,但文件名发生了变化……如何从文件中获取标题……

 public boolean shouldOverrideUrlLoading(WebView view, String url) {
// handle different requests for different type of files
// this example handles downloads requests for .m4a and .mp3 files
// everything else the webview can handle normally
if (url.endsWith(".m4a")) {
Uri source = Uri.parse(url);
// Make a new request pointing to the .apk url
DownloadManager.Request request = new DownloadManager.Request(source);
// appears the same in Notification bar while downloading
request.setDescription("Description for the DownloadManager Bar");
request.setTitle(getTitle());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
else if(url.endsWith(".mp3")) {
// if the link points to an .mp3 resource do something else
}
// if there is a link to anything else than .m4a or .mp3 load the URL in the webview
else view.loadUrl(url);
return true;
}
});

最佳答案

这会给你你的文件名

final String[] separated = url.split("/");
final String myFile = separated[separated.length - 1];

它将使用/字符拆分 url,然后您获取返回数组中的最后一个元素。

数组是从 0 开始的,所以最后一个元素是位于向量长度 - 1 处的元素。

将上面的代码放在这一行之前:if (url.endsWith(".m4a")) {,您要获取文件名的位置。

然后,这样使用它:

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, myFile);

关于android - 下载时我没有得到默认文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com