I'm trying to download an image from a URL using O...
# multiplatform
y
I'm trying to download an image from a URL using Okio in Kotlin Multiplatform, but I couldn't find any examples or documentation on how to do this. I have a function that retrieves a wallpaper download URL, and I want to use Okio to download the image. Here's what I have so far:
Copy code
fun downloadWallpaper(
    wallpaperId: String
) {
    getWallpaperDownloadUrl(wallpaperId) { wallpaperUrl ->
        // Logic to download image
        addMessage(InfoUiMessage(message = Res.string.wallpaper_downloaded))
    }
}
any help would be appreciated
j
Okio is an I/O library not an HTTP client
An HTTP client might use Okio for sending data and receiving data, but Okio on its own cannot download things
y
is there any library I can use, or any implementation?
a
#ktor
y
I found this code, but Kotlin Multiplatform common main doesn't support Java file.
s
You can't use
java.net.URL
in KMP code as that class is JVM-only, not multiplatform. Usually multiplatform code simply uses a string as the URL, so you need to convert
java.net.URL
to a string first.
y
thanks, I'll try your approach
m
Your question is about image loading. Have you tried any of the many specialised image loading libraries? https://github.com/coil-kt/coil https://github.com/Kamel-Media/Kamel https://github.com/qdsfdhvh/compose-imageloader
👍 1
c
Generally people use Coil or above mentioned libraries for image loading and showing, or the #ktor client for a KMM friendly Http requests (I think Coil uses Ktor under the hood by default)
1
👍 2
2