diff --git a/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaver.kt b/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaver.kt index 3b92c04..966edce 100644 --- a/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaver.kt +++ b/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaver.kt @@ -3,7 +3,6 @@ package carnegietechnologies.gallery_saver import android.Manifest import android.app.Activity import android.content.pm.PackageManager -import android.graphics.BitmapFactory import androidx.core.app.ActivityCompat import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel @@ -25,18 +24,6 @@ class GallerySaver internal constructor(private val activity: Activity) : private val job = Job() private val uiScope = CoroutineScope(Dispatchers.Main + job) - /** - * Checks if the file specified by the [path] is an image. - * - * The function returns true/false depending on whether the bounds can be read. - */ - internal fun isImage(path: String): Boolean { - val options = BitmapFactory.Options() - options.inJustDecodeBounds = true - BitmapFactory.decodeFile(path, options) - return (options.outWidth != -1 && options.outHeight != -1) - } - /** * Saves image or video to device * diff --git a/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaverPlugin.kt b/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaverPlugin.kt index 03c8182..089f05b 100644 --- a/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaverPlugin.kt +++ b/android/src/main/kotlin/carnegietechnologies/gallery_saver/GallerySaverPlugin.kt @@ -26,12 +26,6 @@ class GallerySaverPlugin private constructor( when (call.method) { "saveImage" -> gallerySaver.checkPermissionAndSaveFile(call, result, MediaType.image) "saveVideo" -> gallerySaver.checkPermissionAndSaveFile(call, result, MediaType.video) - "image.check" -> { - val path = call.arguments() - val isImage = gallerySaver.isImage(path) - - result.success(isImage) - } else -> result.notImplemented() } } diff --git a/ios/Classes/SwiftGallerySaverPlugin.swift b/ios/Classes/SwiftGallerySaverPlugin.swift index 6e7e56d..78acaaf 100644 --- a/ios/Classes/SwiftGallerySaverPlugin.swift +++ b/ios/Classes/SwiftGallerySaverPlugin.swift @@ -22,27 +22,11 @@ public class SwiftGallerySaverPlugin: NSObject, FlutterPlugin { self.saveMedia(call, .image, result) } else if call.method == "saveVideo" { self.saveMedia(call, .video, result) - } else if call.method == "image.check" { - checkImage(call, result) } else { result(FlutterMethodNotImplemented) } } - /// Attempts to read the size of a image specified as the single argument to the `call`. - /// - /// - Parameters: - /// - call: method object with params for saving media - /// - result: flutter result that gets sent back to the dart code - /// - func checkImage(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { - let path = call.arguments as! String - let url = URL(fileURLWithPath: path) - let size = SwiftGallerySaverPlugin.sizeOfImageAt(url: url) - - result(size != nil) - } - /// Tries to save image to the photos app. /// If user hasn't already permitted saving to the photos, it will be requested /// to do so. @@ -138,24 +122,5 @@ public class SwiftGallerySaverPlugin: NSObject, FlutterPlugin { } } } - - private static func sizeOfImageAt(url: URL) -> CGSize? { - // with CGImageSource we avoid loading the whole image into memory - guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else { - return nil - } - - let propertiesOptions = [kCGImageSourceShouldCache: false] as CFDictionary - guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, propertiesOptions) as? [CFString: Any] else { - return nil - } - - if let width = properties[kCGImagePropertyPixelWidth] as? CGFloat, - let height = properties[kCGImagePropertyPixelHeight] as? CGFloat { - return CGSize(width: width, height: height) - } else { - return nil - } - } } diff --git a/lib/files.dart b/lib/files.dart index 83f3d8d..debc453 100644 --- a/lib/files.dart +++ b/lib/files.dart @@ -9,7 +9,15 @@ const List videoFormats = [ '.mkv', '.flv' ]; - +const List imageFormats = [ + '.jpeg', + '.png', + '.jpg', + '.gif', + '.webp', + '.tif', + '.heic' +]; const http = 'http'; bool isLocalFilePath(String path) { @@ -19,3 +27,6 @@ bool isLocalFilePath(String path) { bool isVideo(String path) => videoFormats.contains(extension(path).toLowerCase()); + +bool isImage(String path) => + imageFormats.contains(extension(path).toLowerCase()); diff --git a/lib/gallery_saver.dart b/lib/gallery_saver.dart index c22aa83..77d0e5f 100644 --- a/lib/gallery_saver.dart +++ b/lib/gallery_saver.dart @@ -46,7 +46,7 @@ class GallerySaver { if (path == null || path.isEmpty) { throw ArgumentError(pleaseProvidePath); } - if (!await _checkIfFileIsImage(path)) { + if (!isImage(path)) { throw ArgumentError(fileIsNotImage); } if (!isLocalFilePath(path)) { @@ -65,10 +65,6 @@ class GallerySaver { return result; } - static Future _checkIfFileIsImage(String path) async { - return _channel.invokeMethod('image.check', path); - } - static Future _downloadFile(String url) async { print(url); http.Client _client = new http.Client();