Android ContentResolver.loadThumbnail转Kotlin

发布于:2024-07-27 ⋅ 阅读:(43) ⋅ 点赞:(0)

Android ContentResolver.loadThumbnail转Kotlin

loadThumbnail原先是Java实现的,现在抠出来转Kotlin实现。

    private fun loadThumbnail(uri: Uri, size: Size, signal: CancellationSignal): Bitmap {
        return myLoadThumbnail(mContext?.contentResolver!!, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE)
    }

    private fun myLoadThumbnail(cr: ContentResolver, uri: Uri, size: Size, signal: CancellationSignal, allocator: Int): Bitmap {
        val opts = Bundle()
        opts.putParcelable(ContentResolver.EXTRA_SIZE, Point(size.width, size.height))
        val orientation = Int64Ref(0)

        var bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource {
            val afd: AssetFileDescriptor = cr.openTypedAssetFile(
                uri, "image/*", opts,
                signal
            )!!
            val extras = afd.extras
            orientation.value = (extras?.getInt(DocumentsContract.EXTRA_ORIENTATION, 0) ?: 0).toLong()
            afd
        }) { decoder: ImageDecoder, info: ImageInfo, source: ImageDecoder.Source? ->
            decoder.allocator = allocator

            // One last-ditch check to see if we've been canceled.
            if (signal != null) signal.throwIfCanceled()

            // We requested a rough thumbnail size, but the remote size may have
            // returned something giant, so defensively scale down as needed.
            val widthSample = info.size.width / size.width
            val heightSample = info.size.height / size.height
            val sample = Math.max(widthSample, heightSample)
            if (sample > 1) {
                decoder.setTargetSampleSize(sample)
            }
        }

        // Transform the bitmap if requested. We use a side-channel to
        // communicate the orientation, since EXIF thumbnails don't contain
        // the rotation flags of the original image.

        // Transform the bitmap if requested. We use a side-channel to
        // communicate the orientation, since EXIF thumbnails don't contain
        // the rotation flags of the original image.
        if (orientation.value != 0L) {
            val width = bitmap.width
            val height = bitmap.height
            val m = Matrix()
            m.setRotate(orientation.value.toFloat(), (width / 2).toFloat(), (height / 2).toFloat())
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false)
        }

        return bitmap
    }

或者:

    private fun loadThumbnail(uri: Uri, size: Size, signal: CancellationSignal): Bitmap {
        return myLoadThumbnail(mContext?.contentResolver!!, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE)
    }

    private fun myLoadThumbnail(cr: ContentResolver, uri: Uri, size: Size, signal: CancellationSignal, allocator: Int): Bitmap {
        val opts = Bundle()
        opts.putParcelable(ContentResolver.EXTRA_SIZE, Point(size.width, size.height))
        val orientation = Int64Ref(0)

        val afd: AssetFileDescriptor = cr.openTypedAssetFile(
            uri, "image/*", opts,
            signal
        )!!

        val src = ImageDecoder.createSource {
            val extras = afd.extras
            orientation.value = (extras?.getInt(DocumentsContract.EXTRA_ORIENTATION, 0) ?: 0).toLong()
            afd
        }

        var bmp = ImageDecoder.decodeBitmap(src, ImageDecoder.OnHeaderDecodedListener { decoder, info, source ->
            decoder.allocator = allocator

            val widthSample = info.size.width / size.width
            val heightSample = info.size.height / size.height
            val sample = Math.max(widthSample, heightSample)
            if (sample > 1) {
                decoder.setTargetSampleSize(sample)
            }
        })

        if (orientation.value != 0L) {
            val width = bmp.width
            val height = bmp.height
            val m = Matrix()
            m.setRotate(orientation.value.toFloat(), (width / 2).toFloat(), (height / 2).toFloat())
            bmp = Bitmap.createBitmap(bmp, 0, 0, width, height, m, false)
        }

        return bmp
    }

Android loadThumbnail ThumbnailUtils.createVideoThumbnail MediaMetadataRetriever time cost, Kotlin_android resolver.loadthumbnail()-CSDN博客文章浏览阅读712次,点赞21次,收藏12次。Android设置头像,手机拍照或从本地相册选取图片作为头像_android 头像拍照_zhangphil的博客-CSDN博客。【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。_android resolver.loadthumbnail()https://blog.csdn.net/zhangphil/article/details/139985203

Android loadThumbnail ThumbnailUtils.createImageThumbnail BitmapFactory.decodeFile time cost, Kotlin-CSDN博客文章浏览阅读776次,点赞16次,收藏12次。从很小的宽高开始,不断迭代增加setRectToRect的目标RectF的宽高,每次迭代加上一定时延,实现Matrix基础上的动画。【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。_loadthumbnailhttps://blog.csdn.net/zhangphil/article/details/139962276