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
}