Last active
February 19, 2021 21:32
-
-
Save jeziellago/5773477fc0f3e77c9316fe1e7493b6d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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