How do I download using Kotlin?

If you do this steps in Kotlin you can Download files from any WebView

1- Add these to your Manifest

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

2- Add these settings to your webview

val webViewSettings = binding.your@id@webview(InsideXML).settings 

        webViewSettings.javaScriptEnabled = true //Needed 

        webViewSettings.domStorageEnabled = true //Needed 

        webViewSettings.allowFileAccess = true //Needed 

        webViewSettings.loadsImagesAutomatically = true //Needed 

----Yo can have more here but this are needed ----- 

3-Handle the downloads

   //handle downloading 

        binding.wvBase.setDownloadListener { url, userAgent, contentDisposition, mimeType, _ -> 

            val request = DownloadManager.Request( 

                Uri.parse(url) 

            ) 

            request.setMimeType(mimeType) 

            val cookies: String = CookieManager.getInstance().getCookie(url) 

            request.addRequestHeader("cookie", cookies) 

            request.addRequestHeader("User-Agent", userAgent) 

           // request.setDescription("Downloading File...") 

            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType)) 

            // request.allowScanningByMediaScanner() 

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) 

            request.setDestinationInExternalPublicDir( 

                Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName( 

                    url, contentDisposition, mimeType 

                ) 

            ) 

            val dm = getSystemService(DOWNLOAD_SERVICE) as DownloadManager 

            dm.enqueue(request) 

            alertdialog() 

        } 

In my case I have a custom alert Dialog to control if user says no, yes.

How do I download from Kotlin?

Download file from remote URL in Kotlin.
Using FileChannel. transferFrom() function. ... .
Using Files. copy() function. ... .
Using BufferedInputStream. Finally, we can read the file from the input stream byte-by-byte and write the bytes to a file output stream..

How do I use Kotlin download manager?

Example 2: Kotlin Android simple DownloadManager Example.
Step 1: Create Project. Start by creating an empty AndroidStudio project..
Step 2: Dependencies. No special third party library is needed for this project..
Step 3: Design Layout. ... .
Step 4: Write Code. ... .
Step 5: Add permissions..

How do I run Kotlin app?

In the Welcome to Android Studio dialog, click Start a new Android Studio project..
Select Basic Activity (not the default). Click Next..
Give your application a name, such as My First App..
Make sure the Language is set to Kotlin..
Leave the defaults for the other fields..
Click Finish..

How do I download from URL?

Download a file.
Most files: Click on the download link. Or, right-click on the file and choose Save as..
Images: Right-click on the image and choose Save Image As..
Videos: Point to the video. Click Download . ... .
PDFs: Right-click on the file and choose Save Link As..
Webpages: At the top right, click More. More Tools..