From 8712a58ed489eabaa2c5d5dc740a31829ed41340 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 26 Feb 2022 10:01:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/DownloadService.kt | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index ebe68c7fa..1c96179cd 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -83,18 +83,25 @@ class DownloadService : BaseService() { toastOnUi("已在下载列表") return } - // 指定下载地址 - val request = DownloadManager.Request(Uri.parse(url)) - // 设置通知 - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN) - // 设置下载文件保存的路径和文件名 - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) - // 添加一个下载任务 - val downloadId = downloadManager.enqueue(request) - downloads[downloadId] = Pair(url, fileName) - queryState() - if (upStateJob == null) { - checkDownloadState() + kotlin.runCatching { + // 指定下载地址 + val request = DownloadManager.Request(Uri.parse(url)) + // 设置通知 + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN) + // 设置下载文件保存的路径和文件名 + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) + // 添加一个下载任务 + val downloadId = downloadManager.enqueue(request) + downloads[downloadId] = Pair(url, fileName) + queryState() + if (upStateJob == null) { + checkDownloadState() + } + }.onFailure { + when (it) { + is SecurityException -> toastOnUi("下载出错,没有存储权限") + else -> toastOnUi("下载出错,${it.localizedMessage}") + } } }