Skip to content

Instantly share code, notes, and snippets.

@jeziellago
Last active February 19, 2021 21:32
Show Gist options
  • Save jeziellago/5773477fc0f3e77c9316fe1e7493b6d7 to your computer and use it in GitHub Desktop.
Save jeziellago/5773477fc0f3e77c9316fe1e7493b6d7 to your computer and use it in GitHub Desktop.
class ImageFileThread(
private val mainHandler: Handler,
private val onAvailableToSendMessage: Handler.() -> Unit
) : Thread() {
private var handler: Handler? = null
override fun run() {
Looper.prepare()
handler = Handler(checkNotNull(Looper.myLooper())) { msg: Message ->
// get data from received message
val imageUrl = msg.data.getString("image_url")
// do some work here
val imageFilePath = getImageFileFromUrl(imageUrl).absolutePath
// create message object and put the result from work
val result = Message().apply { data = bundleOf("image_file" to imageFilePath) }
// send result to main thread
mainHandler.sendMessage(result)
true
}
handler?.onAvailableToSendMessage()
Looper.loop()
}
private fun getImageFileFromUrl(url: String?): File { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment