简体   繁体   中英

Right way for use kotlin coroutine

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) })
    }

}

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM