add optional mediumType parameter of getAlbumThumbnail api method to display video thumbnail correctly

This commit is contained in:
Wenqi Li 2020-09-20 18:34:26 +08:00
parent c70dd411dc
commit ce9184372b
5 changed files with 37 additions and 3 deletions

View File

@ -122,10 +122,11 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
"getAlbumThumbnail" -> { "getAlbumThumbnail" -> {
val albumId = call.argument<String>("albumId") val albumId = call.argument<String>("albumId")
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")
BackgroundAsyncTask({ BackgroundAsyncTask({
getAlbumThumbnail(albumId!!, width, height) getAlbumThumbnail(albumId!!, mediumType, width, height)
}, { v -> }, { v ->
result.success(v) result.success(v)
}) })
@ -461,7 +462,22 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
return byteArray return byteArray
} }
private fun getAlbumThumbnail(albumId: String, width: Int?, height: Int?): ByteArray? { private fun getAlbumThumbnail(albumId: String, mediumType: String?, width: Int?, height: Int?): ByteArray? {
return when (mediumType) {
imageType -> {
getImageAlbumThubnail(albumId, width, height)
}
videoType -> {
getVideoAlbumThubnail(albumId, width, height)
}
else -> {
getImageAlbumThubnail(albumId, width, height)
?: getVideoAlbumThubnail(albumId, width, height)
}
}
}
private fun getImageAlbumThubnail(albumId: String, width: Int?, height: Int?): ByteArray? {
return this.context?.run { return this.context?.run {
val imageCursor = this.contentResolver.query( val imageCursor = this.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
@ -478,6 +494,12 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
} }
return null
}
}
private fun getVideoAlbumThubnail(albumId: String, width: Int?, height: Int?): ByteArray? {
return this.context?.run {
val videoCursor = this.contentResolver.query( val videoCursor = this.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
arrayOf(MediaStore.Video.Media._ID), arrayOf(MediaStore.Video.Media._ID),
@ -493,7 +515,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
} }
} }
return@run null return null
} }
} }

View File

@ -52,11 +52,13 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
else if(call.method == "getAlbumThumbnail") { else if(call.method == "getAlbumThumbnail") {
let arguments = call.arguments as! Dictionary<String, AnyObject> let arguments = call.arguments as! Dictionary<String, AnyObject>
let albumId = arguments["albumId"] as! String let albumId = arguments["albumId"] as! String
let mediumType = arguments["mediumType"] as? String
let width = arguments["width"] as? Int let width = arguments["width"] as? Int
let height = arguments["height"] as? Int let height = arguments["height"] as? Int
let highQuality = arguments["highQuality"] as? Bool let highQuality = arguments["highQuality"] as? Bool
getAlbumThumbnail( getAlbumThumbnail(
albumId: albumId, albumId: albumId,
mediumType: mediumType,
width: width, width: width,
height: height, height: height,
highQuality: highQuality, highQuality: highQuality,
@ -251,6 +253,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
private func getAlbumThumbnail( private func getAlbumThumbnail(
albumId: String, albumId: String,
mediumType: String?,
width: Int?, width: Int?,
height: Int?, height: Int?,
highQuality: Bool?, highQuality: Bool?,
@ -258,6 +261,9 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
) { ) {
let manager = PHImageManager.default() let manager = PHImageManager.default()
let fetchOptions = PHFetchOptions() let fetchOptions = PHFetchOptions()
if (mediumType != nil) {
fetchOptions.predicate = self.predicateFromMediumType(mediumType: mediumType!)
}
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if #available(iOS 9, *) { if #available(iOS 9, *) {
fetchOptions.fetchLimit = 1 fetchOptions.fetchLimit = 1

View File

@ -85,6 +85,7 @@ class PhotoGallery {
/// Get album thumbnail by album id /// Get album thumbnail by album id
static Future<List<dynamic>> getAlbumThumbnail({ static Future<List<dynamic>> getAlbumThumbnail({
@required String albumId, @required String albumId,
MediumType mediumType,
int width, int width,
int height, int height,
bool highQuality, bool highQuality,
@ -92,6 +93,7 @@ class PhotoGallery {
assert(albumId != null); assert(albumId != null);
final bytes = await _channel.invokeMethod('getAlbumThumbnail', { final bytes = await _channel.invokeMethod('getAlbumThumbnail', {
'albumId': albumId, 'albumId': albumId,
'mediumType': mediumTypeToJson(mediumType),
'width': width, 'width': width,
'height': height, 'height': height,
'highQuality': highQuality, 'highQuality': highQuality,

View File

@ -4,12 +4,14 @@ part of photogallery;
class AlbumThumbnailProvider extends ImageProvider<AlbumThumbnailProvider> { class AlbumThumbnailProvider extends ImageProvider<AlbumThumbnailProvider> {
const AlbumThumbnailProvider({ const AlbumThumbnailProvider({
@required this.albumId, @required this.albumId,
this.mediumType,
this.height, this.height,
this.width, this.width,
this.highQuality = false, this.highQuality = false,
}) : assert(albumId != null); }) : assert(albumId != null);
final String albumId; final String albumId;
final MediumType mediumType;
final int height; final int height;
final int width; final int width;
final bool highQuality; final bool highQuality;
@ -30,6 +32,7 @@ class AlbumThumbnailProvider extends ImageProvider<AlbumThumbnailProvider> {
assert(key == this); assert(key == this);
final bytes = await PhotoGallery.getAlbumThumbnail( final bytes = await PhotoGallery.getAlbumThumbnail(
albumId: albumId, albumId: albumId,
mediumType: mediumType,
height: height, height: height,
width: width, width: width,
highQuality: highQuality, highQuality: highQuality,

View File

@ -51,6 +51,7 @@ class Album {
}) { }) {
return PhotoGallery.getAlbumThumbnail( return PhotoGallery.getAlbumThumbnail(
albumId: id, albumId: id,
mediumType: this.mediumType,
width: width, width: width,
height: height, height: height,
highQuality: highQuality, highQuality: highQuality,