diff --git a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt index 596917d..eaeb0bc 100644 --- a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt +++ b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt @@ -122,10 +122,11 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { } "getAlbumThumbnail" -> { val albumId = call.argument("albumId") + val mediumType = call.argument("mediumType") val width = call.argument("width") val height = call.argument("height") BackgroundAsyncTask({ - getAlbumThumbnail(albumId!!, width, height) + getAlbumThumbnail(albumId!!, mediumType, width, height) }, { v -> result.success(v) }) @@ -461,7 +462,22 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { 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 { val imageCursor = this.contentResolver.query( 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( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, arrayOf(MediaStore.Video.Media._ID), @@ -493,7 +515,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { } } - return@run null + return null } } diff --git a/ios/Classes/SwiftPhotoGalleryPlugin.swift b/ios/Classes/SwiftPhotoGalleryPlugin.swift index 5b53b56..5557628 100644 --- a/ios/Classes/SwiftPhotoGalleryPlugin.swift +++ b/ios/Classes/SwiftPhotoGalleryPlugin.swift @@ -52,11 +52,13 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { else if(call.method == "getAlbumThumbnail") { let arguments = call.arguments as! Dictionary let albumId = arguments["albumId"] as! String + let mediumType = arguments["mediumType"] as? String let width = arguments["width"] as? Int let height = arguments["height"] as? Int let highQuality = arguments["highQuality"] as? Bool getAlbumThumbnail( albumId: albumId, + mediumType: mediumType, width: width, height: height, highQuality: highQuality, @@ -251,6 +253,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { private func getAlbumThumbnail( albumId: String, + mediumType: String?, width: Int?, height: Int?, highQuality: Bool?, @@ -258,6 +261,9 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { ) { let manager = PHImageManager.default() let fetchOptions = PHFetchOptions() + if (mediumType != nil) { + fetchOptions.predicate = self.predicateFromMediumType(mediumType: mediumType!) + } fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] if #available(iOS 9, *) { fetchOptions.fetchLimit = 1 diff --git a/lib/photo_gallery.dart b/lib/photo_gallery.dart index 04dcf4c..abcc609 100644 --- a/lib/photo_gallery.dart +++ b/lib/photo_gallery.dart @@ -85,6 +85,7 @@ class PhotoGallery { /// Get album thumbnail by album id static Future> getAlbumThumbnail({ @required String albumId, + MediumType mediumType, int width, int height, bool highQuality, @@ -92,6 +93,7 @@ class PhotoGallery { assert(albumId != null); final bytes = await _channel.invokeMethod('getAlbumThumbnail', { 'albumId': albumId, + 'mediumType': mediumTypeToJson(mediumType), 'width': width, 'height': height, 'highQuality': highQuality, diff --git a/lib/src/image_providers/album_thumbnail_provider.dart b/lib/src/image_providers/album_thumbnail_provider.dart index 5fbf7fc..97dfbfd 100644 --- a/lib/src/image_providers/album_thumbnail_provider.dart +++ b/lib/src/image_providers/album_thumbnail_provider.dart @@ -4,12 +4,14 @@ part of photogallery; class AlbumThumbnailProvider extends ImageProvider { const AlbumThumbnailProvider({ @required this.albumId, + this.mediumType, this.height, this.width, this.highQuality = false, }) : assert(albumId != null); final String albumId; + final MediumType mediumType; final int height; final int width; final bool highQuality; @@ -30,6 +32,7 @@ class AlbumThumbnailProvider extends ImageProvider { assert(key == this); final bytes = await PhotoGallery.getAlbumThumbnail( albumId: albumId, + mediumType: mediumType, height: height, width: width, highQuality: highQuality, diff --git a/lib/src/models/album.dart b/lib/src/models/album.dart index 27b9597..772ea0e 100644 --- a/lib/src/models/album.dart +++ b/lib/src/models/album.dart @@ -51,6 +51,7 @@ class Album { }) { return PhotoGallery.getAlbumThumbnail( albumId: id, + mediumType: this.mediumType, width: width, height: height, highQuality: highQuality,