问题描述
我正在尝试查找已经通过Downloadmanager
下载的特定文件。我知道此文件存储在Enviroment.DIRECTORY_DOWNLOADS
中。但是由于以下原因,我不知道如何获取此特定文件:
- 如何确保以最快,最安全的方式查找文件,
- 当文件有两个版本(例如
MyFile
和MyFile1
)时,如何查找文件, - 如何确保从API 21到API 30都能正常工作?,
- 找不到文件时返回null
- 我是否需要寻求权限才能找到我的文件(甚至必须这样做)?
这是我目前的方法:
我的Downloadmanager(成功将文件保存在下载目录中)
private fun initDownload(uri: Uri,fileName: String) {
val downloadManager = context.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(uri)
.setTitle(fileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_DOWNLOADS,"$fileName.pdf")
downloadManager.enqueue(request)
}
查找文件(尚未测试)
class FileValidator @Inject constructor(@ApplicationContext val context: Context) {
fun getFileOrNull(fileName: String): File? {
// Have to create a generic regex to find every possible file that matches my fileName,don't know how
val regex = fileName
val downloadFolder = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
val fileList = downloadFolder?.listFiles()
if (fileList != null) {
for (file in fileList) {
return if(file.name == regex) {
file
} else {
null
}
}
}
return null
}
}
感谢您的帮助,谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)