So after 3 days of wanting to jump off a cliff. I found the answer. Of course it was a few minutes after asking the question here (first question ever so be kind.). The only Issue is you need a SSL cert for HTTPS on the server retrieving the file. My server is http but i can get a cert in there and fix that. to Test i threw up a github repository and linked to the raw text file. Here is my solution if this saves you 3 days pour one out for me.

Thread {
            try {
                val url = URL("https://raw.githubusercontent.com/USERNAME/NeedHTTPSdontWanaSSL/main/info.txt")
                val uc: HttpsURLConnection = url.openConnection() as HttpsURLConnection
                val br = BufferedReader(InputStreamReader(uc.getInputStream()))
                var line: String?
                val lin2 = StringBuilder()
                while (br.readLine().also { line = it } != null) {
                    lin2.append(line)
                }
                Log.d("The Text", "$lin2")
            } catch (e: IOException) {
                Log.d("texts", "onClick: " + e.getLocalizedMessage())
                e.printStackTrace()
            }
        }.start()

Credit: answered Aug 12, 2018 at 9:29 Aishik kirtaniya Android - How can I read a text file from a url?

Answer from GraduatedImposterSynd on Stack Overflow
Top answer
1 of 2
3

So after 3 days of wanting to jump off a cliff. I found the answer. Of course it was a few minutes after asking the question here (first question ever so be kind.). The only Issue is you need a SSL cert for HTTPS on the server retrieving the file. My server is http but i can get a cert in there and fix that. to Test i threw up a github repository and linked to the raw text file. Here is my solution if this saves you 3 days pour one out for me.

Thread {
            try {
                val url = URL("https://raw.githubusercontent.com/USERNAME/NeedHTTPSdontWanaSSL/main/info.txt")
                val uc: HttpsURLConnection = url.openConnection() as HttpsURLConnection
                val br = BufferedReader(InputStreamReader(uc.getInputStream()))
                var line: String?
                val lin2 = StringBuilder()
                while (br.readLine().also { line = it } != null) {
                    lin2.append(line)
                }
                Log.d("The Text", "$lin2")
            } catch (e: IOException) {
                Log.d("texts", "onClick: " + e.getLocalizedMessage())
                e.printStackTrace()
            }
        }.start()

Credit: answered Aug 12, 2018 at 9:29 Aishik kirtaniya Android - How can I read a text file from a url?

2 of 2
0

How about this?

import java.net.URL

val s = "https://www.someplace.com/dir/file.txt"
val text = URL(s).openStream().readAllBytes().decodeToString()

Wrap it in some exception handling if you want. So long as you don't keep references to the intermediate stream, it'll immediately be eligible for garbage collection. The stream will be closed when it's garbage collected, thus I don't bother with adding a call to explicitly close it.

🌐
DEV Community
dev.to › arkilis › download-image-from-url-in-kotlin-4fc6
Download image from URL in Kotlin - DEV Community
February 20, 2023 - Here's an example of how to use the URL and readBytes to download an image from a URL in Kotlin: import java.net.URL import kotlin.io.readBytes fun main() { val url = URL("https://www.example.com/image.png") val imageData = url.readBytes() // TODO: ...
Discussions

Help enabling downloads through webview (Kotlin)
Good day. I'm new to android development and I'm trying to develop a simple webview application, picked a nice template and went through the steps and made good progress, I managed to load my site fully and enable javascript, that works as... More on xdaforums.com
🌐 xdaforums.com
0
April 9, 2021
Can I convert url to File?
While you don't need a File instance, can pass the String directly, it probably won't work as you expect. That's not really about Kotlin but the Telegram API. The send video method is for sending files, either local or providing a URL where Telegram can download the file. It is not valid for YouTube or other media streaming services since the URL is not a file for downloading but a link to the player. https://core.telegram.org/bots/api#sendvideo I guess you can send a plain text message with the YouTube video HTTPS URL, although I don't know if the Telegram client will embed the YouTube player or not. Maybe there's another method for embedded content. Apparently there's an inline mode to embed content, maybe that's what you need for YouTube videos, not really sure since I've never used Telegram... but there's an API for embedding videos like YouTube in that mode. Edit: Refactored and clarified More on reddit.com
🌐 r/Kotlin
4
2
March 7, 2022
Download file from url in android using retrofit
You might want to skip Retrofit and do straight OkHttp for downloading things like this. For one, you can avoid the AsyncTask and put the download code directly on the OkHttp's callback. The other thing that will vastly improve performance is by using Okio's streams and not java.io streams. Given a ResponseBody, the correct way to write to a File (without progress) is: try (BufferedSink sink = Okio.buffer(Okio.sink(file))) { sink.writeAll(responseBody.source()); } If you want progress you can adapt this sample from OkHttp: https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/Progress.java More on reddit.com
🌐 r/androiddev
2
0
January 8, 2019
How to Extract the File Name from URI in Android 14?
When reaching out to ContentProvider, you can't know what is the real file name that it handles. It might not even handle a file so reaching the path might also be impossible. Could be all stored on memory for example, with some unique name it decided. Can you please share a sample project to check it out? What is the app that you've chosen to reach to ? What do you get when you reach this? More on reddit.com
🌐 r/AndroidStudio
4
1
March 22, 2024
🌐
Atomic Spin
spin.atomicobject.com › android-download-files-kotlin
Download Files in Kotlin for Android Using Ktor and Intents
June 18, 2025 - Downloading the file opens the output stream to the URI given and dispatches the download file coroutine. The download itself is handled on the IO thread, but the emitter results are handled on the Main thread.
🌐
CodeSignal
codesignal.com › learn › courses › building-robust-api-clients-in-kotlin › lessons › downloading-files-from-an-api-1
Downloading Files from an API
Here's a basic example of downloading a file named welcome.txt from our API at http://localhost:8000/notes. This approach downloads the entire file at once, which is manageable for smaller files. This code sends a GET request and writes the full response content to a local file. This method works well for small files but can strain memory for larger files.
🌐
Kotlin Academy
blog.kotlin-academy.com › download-files-with-ktor-and-coroutines-e96b1cc8b657
Download files with Ktor client, Koin and Coroutines | by Francesco Gatto | kt.academy
May 20, 2020 - The core of this method is the downloadFile extension function. It is a suspend function and receives the file where writes bytes downloaded from URL with a callback with the result of the operation:
🌐
Medium
medium.com › mobile-app-development-publication › download-file-in-android-with-kotlin-874d50bccaa2
Download File in Android with Kotlin | by Elye - A Dev By Grace | Mobile App Development Publication | Medium
September 5, 2020 - Here I will show you how to download with some simple code sample. First and foremost, you need to enable your App to access to the internet · Add the below permission in your AndroidManifest.xml file.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › android › how-to-download-file-from-url-in-android-programmatically-using-download-manager
How to Download File from URL in Android Programmatically using Download Manager? - GeeksforGeeks
July 23, 2025 - Kotlin · Java · Flutter · Dart · Android Studio · MVVM · SDK · Last Updated : 23 Jul, 2025 · In this article, we are going to learn how to download files from an URL using Download Manager. Here we will be simply adding the link of the file available online.
🌐
Baeldung
baeldung.com › home › kotlin › download pdf files with retrofit and coroutines
Download PDF Files with Retrofit and Coroutines | Baeldung on Kotlin
December 22, 2024 - Using Retrofit and Kotlin Coroutines is an efficient approach when we want to perform HTTP requests to get resources stored in the cloud. In the next flowchart, we can see the process we’ll apply: Let’s start defining the first component of the process. First, we need to define a service interface for Retrofit that will expose a function for downloading files: interface FileDownloadService { @GET suspend fun downloadFile(@Url fileUrl: String): ResponseBody }
🌐
W3Schools
w3schools.com › html › html_filepaths.asp
HTML File Paths
In the following example, the file path points to a file in the images folder located in the folder one level up from the current folder: ... It is best practice to use relative file paths (if possible). When using relative file paths, your web pages will not be bound to your current base URL.
🌐
The Eclipse Foundation
eclipse.org › downloads
Eclipse Downloads | The Eclipse Foundation
The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 415 open source projects, including runtimes, tools and frameworks.
🌐
XDA Forums
xdaforums.com › home › general development › app development forums › development tools › android studio
Help enabling downloads through webview (Kotlin) | XDA Forums
April 9, 2021 - // Set web view download listener web_view.setDownloadListener(DownloadListener { url, userAgent, contentDescription, mimetype, contentLength -> // Initialize download request val request = DownloadManager.Request(Uri.parse(url)) // Get the cookie val cookies = CookieManager.getInstance().getCookie(url) // Add the download request header request.addRequestHeader("Cookie",cookies) request.addRequestHeader("User-Agent",userAgent) // Set download request description request.setDescription("Downloading requested file....") // Set download request mime tytpe request.setMimeType(mimetype) // Allow s
🌐
The Code City
thecodecity.com › home › android › download pdf from any url in android studio – solution
Download PDF From Any URL in Android Studio – Solution
May 13, 2023 - You can easily download a PDF file from any URL using one of the following two methods. If you want to download the file within the app using DownloadManager...
🌐
Mobikul
mobikul.com › home › how we download file from url in android
How we download File from URL in android - Mobikul
April 22, 2017 - There are many methods to download files from the server. Keep Reading...
🌐
RUTUBE
rutube.ru › video › 17a46e312bb5370ff3ba4330a687a710
How to Download File From URL using Download Manager Kotlin Android Studio
In this video we are going to see how we can download any file from url using download manager in android studio with kotlin as a programming language. How we can download file from url programmatically in android Jetpack Compose is Android’s modern toolkit for building native UI.
Published   November 29, 2023
Views   16
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 16933073 › for-anyone-downloading-files-in-compose-multiplatform-this-w
For anyone downloading files in compose multiplatform this w kotlinlang #multiplatform
) { val request = Request.Builder() .url(url) .header(“Authorization”, “Bearer $authToken”) .build() val client = OkHttpClient() client.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { Log.e(“DownloadFile”, “Error downloading file: ${e.message}“) } override fun onResponse(call: Call, response: Response) { response.body?.let { responseBody -> try { val contentDisposition = response.header(“Content-Disposition”) val fileName = extractFileName(contentDisposition) val directory = File( Environment.getExternalStoragePublicDirector