6

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

I just want to see my files in the files folder shown in the pic below.

Device File Explorer

Raunit Verma
  • 149
  • 2
  • 11

2 Answers2

4

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I am trying to download google drive files with its link, I don't think using Okhttp I can download the Google drive files. – Raunit Verma Mar 03 '22 at 18:09
  • @RaunitVerma: "I don't think using Okhttp I can download the Google drive files" -- I believe that you are mistaken. – CommonsWare Mar 03 '22 at 18:17
  • `request.setDestinationInExternalPublicDir( context.filesDir.absolutePath, URLUtil.guessFileName(url, contentDisposition, mimetype) )` That is impossible code. You are specifying a complete path where you only have to specify a public directory name. A folder name. No path. – blackapps Mar 03 '22 at 19:58
1

request.setDestinationInExternalPublicDir(

Replace by:

 request.setDestinationInExternalFilesDir(
blackapps
  • 8,011
  • 2
  • 11
  • 25
  • I tried it but I cannot see the files downloaded in the `filesDir` from Device File Explorer in Android Studio but I can see it in my phone. Also I cannot access the file using `var file=File(context.filesDir,fileName)` – Raunit Verma Mar 03 '22 at 18:46
  • Here is the code I added `request.setDestinationInExternalFilesDir(context,context.filesDir.absolutePath,fileName)` but I cannot see the file or access it except from the Download App in my Phone. – Raunit Verma Mar 03 '22 at 19:03
  • var file=File(context.externalFilesDir,fileName) – blackapps Mar 03 '22 at 19:54
  • `request.setDestinationInExternalFilesDir(context,context.filesDir.absolutePath,fileName)` That is nonsense. Better: `request.setDestinationInExternalFilesDir(subdir, ,fileName)` – blackapps Mar 03 '22 at 19:55
  • `tried it but I cannot see the files downloaded in the filesDir from Device File Explorer ` Of course not. You would see it in externalFilesDir. – blackapps Mar 03 '22 at 20:00
  • Can you please help me, I just want my downloaded files to be available in the `files` folder shown in the picture above? – Raunit Verma Mar 03 '22 at 20:33
  • CommonsWare told you that that does not go with DownloadManager. DIdnt you read that? – blackapps Mar 03 '22 at 20:50