我的应用程序需要下载文件,我正在查看DownloadManager,但它有一些不适合我的情况(身份验证,命名方案,验证)的限制,所以我制作了自定义下载引擎。
是否可以手动将使用我的引擎下载的文件(因此使用本地URL)添加到下载系统应用程序的列表中?我的理解是列表由系统内容提供者填充。是否可以在没有DownloadManager尝试下载文件的情况下向其中添加记录?
谢谢;)
答案 0 :(得分:5)
要手动添加文件,您需要使用DownloadManager类。我使用以下内容在我在本地创建的下载应用程序中显示文件。
DownloadManager downloadManager = (DownloadManager)mainActivity.getSystemService(mainActivity.DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(file.getName(), file.getName(), true, "application/json", file.getAbsolutePath(),file.length(),true);
这将使文件显示在6.0上的Downloads应用程序中,即使该文件是在本地创建的。
答案 1 :(得分:1)
上述答案的科特琳等效项:
val downloadManager = this.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadManager.addCompletedDownload(file.getName(), file.getName(), true, "application/json", file.getAbsolutePath(),file.length(),true)