I am trying to convert a URL to Base64 in Kotlin. I am using Apache Commons IO to accomplish this by first converting the URL to a ByteArray
:
val imageBytes = IOUtils.toByteArray(URL(url))
This line creates a android.os.NetworkOnMainThreadException
error when I run my app. How do I resolve this? I don't want to change the ThreadPolicy
to bypass the error. Below the above line of code, I also have the following:
val imageBase64 = Base64.encode(imageBytes, Base64.URL_SAFE and Base64.NO_WRAP)
val item = ItemCard(
imageBase64,
label,
total
)
list += item
The entire code block is in a loop. So I would also need something where I can get the result in a timely manner where it doesn't interfere with the actual Base64 encoding.