为什么我们不需要存储权限来通过 DownloadManager 下载文件
我正在通过 android DownloadManager 下载文件,其功能如下。
private fun downloadFile() {
val downloadManager: DownloadManager =
getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request =
DownloadManager.Request(Uri.
parse("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"))
downloadManager.enqueue(request)
}
}
下载正常进行,无需任何存储权限。
下载管理器在没有存储权限的情况下是否可以工作? 我在官方文档中找不到答案。
https://developer.android.com/reference/android/app/DownloadManager
I'm downloading a file through android DownloadManager with de function below.
private fun downloadFile() {
val downloadManager: DownloadManager =
getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request =
DownloadManager.Request(Uri.
parse("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"))
downloadManager.enqueue(request)
}
}
The download occurs normally without any storage permissions.
Does the download manager work without storage permissions?
I can't find the answer in the official documentation.
https://developer.android.com/reference/android/app/DownloadManager
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DownloadManager
将下载工作委托给系统提供的单独应用程序。该应用程序有权写入某些位置,即使您的应用程序没有。DownloadManager
delegates the downloading work to a separate system-supplied app. That app has rights to write to a few locations, even if your app does not.如果你使用 setDestinationInExternalPublicDir
对于定位 Build.VERSION_CODES.Q 或更高版本的应用程序,不需要 WRITE_EXTERNAL_STORAGE 权限,并且 dirType 必须是已知的公共目录之一,例如
环境#DIRECTORY_DOWNLOADS、环境#DIRECTORY_PICTURES、环境#DIRECTORY_MOVIES 等。
对于 setDestinationUri
对于定位 Build.VERSION_CODES.Q 的应用程序或如上所述,不需要 WRITE EXTERNAL_STORAGE 权限,并且 uri 必须引用应用程序拥有的目录中的路径(例如 Context#getExternalFilesDir(String))或顶级下载目录中的路径(由环境#getExternalStoragePublicDirectory(String) 和环境#DIRECTORY_DOWNLOADS)。
If u use setDestinationInExternalPublicDir
For applications targeting Build.VERSION_CODES.Q or above, WRITE_EXTERNAL_STORAGE permission is not needed and the dirType must be one of the known public directories like
Environment#DIRECTORY_DOWNLOADS, Environment#DIRECTORY_PICTURES, Environment#DIRECTORY_MOVIES, etc.
And for setDestinationUri
For applications targeting Build.VERSION_CODES.Q or above, WRITE EXTERNAL_STORAGE permission is not needed and the uri must refer to a path within the directories owned by the application (e.g. Context#getExternalFilesDir(String)) or a path within the top-level Downloads directory (as returned by Environment#getExternalStoragePublicDirectory(String) with Environment#DIRECTORY_DOWNLOADS).