如何将从 Android 中的下载管理器下载的文件保存到应用程序内部(filesDir),因为现在访问 Android 中的文件并不容易

Rau*_*rma 6 java android android-webview kotlin android-download-manager

这是当我从 android 中的 webView 下载任何内容时我尝试执行相同操作的代码。

 webview.setDownloadListener { url, userAgent, contentDisposition, mimetype, l ->
                val request = DownloadManager.Request(Uri.parse(url))
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype))
                request.setDescription("Downloading file...")
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                        var newuri=FileProvider.getUriForFile(context, "com.shyptsolution.classproject"+ ".provider", File(context.filesDir,"fileName"))
//                request.setDestinationUri(newuri)
                request.setDestinationInExternalPublicDir(
                   context.filesDir.absolutePath,
                    URLUtil.guessFileName(url, contentDisposition, mimetype)
                )
                val dm =context.applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
                dm!!.enqueue(request)
            }
Run Code Online (Sandbox Code Playgroud)

但我收到错误,它不是标准目录。如何将文件保存在缓存或文件目录中?

我只想查看files下图所示文件夹中的文件。

设备文件浏览器

Com*_*are 4

DownloadManager无法下载到内部存储,因为它无法访问该位置。

DownloadManager由于范围存储限制,在现代 Android 版本中非常有限。我建议您使用其他工具(例如 OkHttp)将应用程序中的内容下载到您控制的文件中,包括下载到内部存储中。