accept highQuality parameter in "getThumbnail" and "getAlbumThumbnail" API on Android platform

This commit is contained in:
Wenqi Li 2021-04-21 18:53:24 +08:00
parent 7ccffc30d2
commit fe602c1281

View File

@ -120,8 +120,9 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val mediumType = call.argument<String>("mediumType") val mediumType = call.argument<String>("mediumType")
val width = call.argument<Int>("width") val width = call.argument<Int>("width")
val height = call.argument<Int>("height") val height = call.argument<Int>("height")
val highQuality = call.argument<Boolean>("highQuality")
BackgroundAsyncTask({ BackgroundAsyncTask({
getThumbnail(mediumId!!, mediumType, width, height) getThumbnail(mediumId!!, mediumType, width, height, highQuality)
}, { v -> }, { v ->
result.success(v) result.success(v)
}) })
@ -131,8 +132,9 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val mediumType = call.argument<String>("mediumType") val mediumType = call.argument<String>("mediumType")
val width = call.argument<Int>("width") val width = call.argument<Int>("width")
val height = call.argument<Int>("height") val height = call.argument<Int>("height")
val highQuality = call.argument<Boolean>("highQuality")
BackgroundAsyncTask({ BackgroundAsyncTask({
getAlbumThumbnail(albumId!!, mediumType, width, height) getAlbumThumbnail(albumId!!, mediumType, width, height, highQuality)
}, { v -> }, { v ->
result.success(v) result.success(v)
}) })
@ -469,40 +471,42 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
return videoMetadata return videoMetadata
} }
private fun getThumbnail(mediumId: String, mediumType: String?, width: Int?, height: Int?): ByteArray? { private fun getThumbnail(mediumId: String, mediumType: String?, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
return when (mediumType) { return when (mediumType) {
imageType -> { imageType -> {
getImageThumbnail(mediumId, width, height) getImageThumbnail(mediumId, width, height, highQuality)
} }
videoType -> { videoType -> {
getVideoThumbnail(mediumId, width, height) getVideoThumbnail(mediumId, width, height, highQuality)
} }
else -> { else -> {
getImageThumbnail(mediumId, width, height) getImageThumbnail(mediumId, width, height, highQuality)
?: getVideoThumbnail(mediumId, width, height) ?: getVideoThumbnail(mediumId, width, height, highQuality)
} }
} }
} }
private fun getImageThumbnail(mediumId: String, width: Int?, height: Int?): ByteArray? { private fun getImageThumbnail(mediumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
var byteArray: ByteArray? = null var byteArray: ByteArray? = null
val bitmap: Bitmap? = this.context?.run { val bitmap: Bitmap? = this.context?.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try { try {
val widthSize = width ?: if (highQuality == true) 512 else 96
val heightSize = height ?: if (highQuality == true) 384 else 96
this.contentResolver.loadThumbnail( this.contentResolver.loadThumbnail(
ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediumId.toLong()), ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediumId.toLong()),
Size(width ?: 512, height ?: 384), Size(widthSize, heightSize),
null null
) )
} catch (e: Exception) { } catch (e: Exception) {
null null
} }
} else { } else {
val kind = if (highQuality == true) MediaStore.Images.Thumbnails.MINI_KIND else MediaStore.Images.Thumbnails.MICRO_KIND
MediaStore.Images.Thumbnails.getThumbnail( MediaStore.Images.Thumbnails.getThumbnail(
this.contentResolver, mediumId.toLong(), this.contentResolver, mediumId.toLong(),
MediaStore.Images.Thumbnails.MINI_KIND, kind, null
null
) )
} }
} }
@ -516,25 +520,27 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
return byteArray return byteArray
} }
private fun getVideoThumbnail(mediumId: String, width: Int?, height: Int?): ByteArray? { private fun getVideoThumbnail(mediumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
var byteArray: ByteArray? = null var byteArray: ByteArray? = null
val bitmap: Bitmap? = this.context?.run { val bitmap: Bitmap? = this.context?.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try { try {
val widthSize = width ?: if (highQuality == true) 512 else 96
val heightSize = height ?: if (highQuality == true) 384 else 96
this.contentResolver.loadThumbnail( this.contentResolver.loadThumbnail(
ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediumId.toLong()), ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediumId.toLong()),
Size(width ?: 512, height ?: 384), Size(widthSize, heightSize),
null null
) )
} catch (e: Exception) { } catch (e: Exception) {
null null
} }
} else { } else {
val kind = if (highQuality == true) MediaStore.Images.Thumbnails.MINI_KIND else MediaStore.Images.Thumbnails.MICRO_KIND
MediaStore.Video.Thumbnails.getThumbnail( MediaStore.Video.Thumbnails.getThumbnail(
this.contentResolver, mediumId.toLong(), this.contentResolver, mediumId.toLong(),
MediaStore.Images.Thumbnails.MINI_KIND, kind,null
null
) )
} }
} }
@ -548,22 +554,22 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
return byteArray return byteArray
} }
private fun getAlbumThumbnail(albumId: String, mediumType: String?, width: Int?, height: Int?): ByteArray? { private fun getAlbumThumbnail(albumId: String, mediumType: String?, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
return when (mediumType) { return when (mediumType) {
imageType -> { imageType -> {
getImageAlbumThumbnail(albumId, width, height) getImageAlbumThumbnail(albumId, width, height, highQuality)
} }
videoType -> { videoType -> {
getVideoAlbumThumbnail(albumId, width, height) getVideoAlbumThumbnail(albumId, width, height, highQuality)
} }
else -> { else -> {
getImageAlbumThumbnail(albumId, width, height) getImageAlbumThumbnail(albumId, width, height, highQuality)
?: getVideoAlbumThumbnail(albumId, width, height) ?: getVideoAlbumThumbnail(albumId, width, height, highQuality)
} }
} }
} }
private fun getImageAlbumThumbnail(albumId: String, width: Int?, height: Int?): ByteArray? { private fun getImageAlbumThumbnail(albumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
return this.context?.run { return this.context?.run {
val imageCursor: Cursor? val imageCursor: Cursor?
@ -608,7 +614,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID) val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID)
val id = cursor.getLong(idColumn) val id = cursor.getLong(idColumn)
return@run getImageThumbnail(id.toString(), width, height) return@run getImageThumbnail(id.toString(), width, height, highQuality)
} }
} }
@ -616,7 +622,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
} }
private fun getVideoAlbumThumbnail(albumId: String, width: Int?, height: Int?): ByteArray? { private fun getVideoAlbumThumbnail(albumId: String, width: Int?, height: Int?, highQuality: Boolean?): ByteArray? {
return this.context?.run { return this.context?.run {
val videoCursor: Cursor? val videoCursor: Cursor?
@ -661,7 +667,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
if (cursor.moveToNext()) { if (cursor.moveToNext()) {
val idColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID) val idColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID)
val id = cursor.getLong(idColumn) val id = cursor.getLong(idColumn)
return@run getVideoThumbnail(id.toString(), width, height) return@run getVideoThumbnail(id.toString(), width, height, highQuality)
} }
} }