简体   繁体   English

我怎样才能将从 Android 中的下载管理器下载的文件保存到应用程序内部(filesDir),因为现在它不容易访问 Android 中的文件

[英]How can I save files downloaded from Download Manager in Android to app internal(filesDir) as now its not easy to access files in Android

Here is the code i am tring to do the same when i am downloading anything from webView in android.这是我在 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)
            }

But I am getting error in this that it is not a standard directory.但是我得到的错误是它不是标准目录。 How can I save the files in cache or filesdir??如何将文件保存在缓存或文件目录中??

I just want to see my files in the files folder shown in the pic below.我只想在下图所示的files夹中查看我的文件。

设备文件资源管理器

DownloadManager cannot download to internal storage , as it has no access to that location. DownloadManager无法下载到内部存储,因为它无权访问该位置。

DownloadManager is very limited in modern versions of Android, due to scoped storage restrictions.由于范围存储限制, DownloadManager在 Android 的现代版本中非常有限。 I recommend that you use something else, such as OkHttp, to download the content within your app to a file that you control, including onto internal storage.我建议您使用其他工具(例如 OkHttp)将应用程序中的内容下载到您控制的文件中,包括内部存储。

request.setDestinationInExternalPublicDir( request.setDestinationInExternalPublicDir(

Replace by:替换为:

 request.setDestinationInExternalFilesDir(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM