0

The following code works perfectly to download mp3 file in external storage, but doens't work to download file in internal storage. A lot of smartphone doesn't have external storage. What can i do? I don't know how to implement async task in kotlin, if it is needed.

 mywebView.setDownloadListener(object : DownloadListener {
        override fun onDownloadStart(url: String, userAgent: String,
                                     contentDisposition: String, mimetype: String,
                                     contentLength: Long) {


            val request = DownloadManager.Request(Uri.parse(url))
            request.allowScanningByMediaScanner()
            request.setDescription("Download file...")
            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype))

            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) //Notify client once download is completed!
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mimetype )
            val webview = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
            webview.enqueue(request)

            Toast.makeText(getApplicationContext(), "Download avviato", Toast.LENGTH_LONG).show()

        }
    })
  • Read [this](https://stackoverflow.com/questions/46610819/how-to-get-absolute-path-of-internal-storage-in-android) and [this](https://stackoverflow.com/questions/39850004/android-get-all-external-storage-path-for-all-devices). So all smartphones have an external storage, not all has SD cards. – Denis Sologub Oct 29 '18 at 16:16

1 Answers1

-1

Cannot test the code at the moment, but it should be something like the follow:

 mywebView.setDownloadListener(object : DownloadListener {
        override fun onDownloadStart(url: String, userAgent: String,
                                     contentDisposition: String, mimetype: String,
                                     contentLength: Long) {


            val request = DownloadManager.Request(Uri.parse(url))
            request.allowScanningByMediaScanner()
            request.setDescription("Download file...")
            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype))

            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) //Notify client once download is completed!
            request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory(), mimetype )
            val webview = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
            webview.enqueue(request)

            Toast.makeText(getApplicationContext(), "Download avviato", Toast.LENGTH_LONG).show()

        }
    })
Legion
  • 760
  • 6
  • 23