Right way for use kotlin coroutine Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Right way for use kotlin coroutine without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am using kotlin coroutinescope for loading thumnails from video uri and add imageview with that bitmaps in linear layout.

Currently after all thumnail loaded, I am adding into linearlayout. Can anyone suggest me for getting one by one bitmao and adding into linearlayout?

private fun loadThumbnails(uri: Uri) {
    val metaDataSource = MediaMetadataRetriever()
    metaDataSource.setDataSource(context, uri)

    val videoLength = (metaDataSource.extractMetadata(
            MediaMetadataRetriever.METADATA_KEY_DURATION).toInt() * 1000).toLong()

    val thumbnailCount = 8

    val interval = videoLength / thumbnailCount

    var listOfImage: ArrayList<Bitmap?> = ArrayList()

    for (i in 0 until thumbnailCount - 1) {

        try {
            var bitmap: Bitmap? = null
            val job = CoroutineScope(Dispatchers.IO).launch {
                val frameTime = i * interval
                bitmap = metaDataSource.getFrameAtTime(frameTime, MediaMetadataRetriever.OPTION_CLOSEST)

                bitmap?.let {
                    val targetWidth: Int
                    val targetHeight: Int
                    if (it.height > it.width) {
                        targetHeight = frameDimension
                        val percentage = frameDimension.toFloat() / it.height
                        targetWidth = (it.width * percentage).toInt()
                    } else {
                        targetWidth = frameDimension
                        val percentage = frameDimension.toFloat() / it.width
                        targetHeight = (it.height * percentage).toInt()
                    }
                    bitmap = Bitmap.createScaledBitmap(it, targetWidth, targetHeight, false)
                }
                listOfImage.add(bitmap)
                metaDataSource.release()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    listOfImage.forEach {
        container_thumbnails.addView(ThumbnailView(context).apply { setImageBitmap(it) })
    }

}

Answer

Please try next approach:

val job = CoroutineScope(Dispatchers.Main).launch {
    val frameTime = i * interval
    val bitmap = loadBitmap(frameTime) // loads bitmap asynchronously using withContext(Dispatchers.IO)

    // ... use bitmap to set into a view
}

suspend fun loadBitmap(frameTime: Int): Bitmap? = withContext(Dispatchers.IO) {
    bitmap = metaDataSource.getFrameAtTime(frameTime, MediaMetadataRetriever.OPTION_CLOSEST)

    bitmap?.let {
        val targetWidth: Int
        val targetHeight: Int
        if (it.height > it.width) {
             targetHeight = frameDimension
             val percentage = frameDimension.toFloat() / it.height
             targetWidth = (it.width * percentage).toInt()
        } else {
             targetWidth = frameDimension
             val percentage = frameDimension.toFloat() / it.width
             targetHeight = (it.height * percentage).toInt()
        }
        Bitmap.createScaledBitmap(it, targetWidth, targetHeight, false)
    }
}
We are here to answer your question about Right way for use kotlin coroutine - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts