Home > Software engineering >  How can I save files downloaded from Download Manager in Android to app internal(filesDir) as now it
How can I save files downloaded from Download Manager in Android to app internal(filesDir) as now it

Time:03-04

Here is the code i am tring to do the same when i am downloading anything from webView in android.

 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??

CodePudding user response:

DownloadManager cannot download to internal storage, as it has no access to that location.

DownloadManager is very limited in modern versions of Android, due to scoped storage restrictions. 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.

CodePudding user response:

request.setDestinationInExternalPublicDir(

Replace by:

 request.setDestinationInExternalFilesDir(
  • Related