diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fc852c..aa84ee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.0.3 +- 1.Upgrade flutter version to 3.10.5 +- 2.Android build tools are upgraded to 7.3.0 +- 3.Optimize the Android plugin library code + ## 2.0.2 - 1.Optimization android plugin diff --git a/README.md b/README.md index 65885de..49bd2a7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ We use the `image_picker` plugin to select images from the Android and iOS image To use this plugin, add `image_gallery_saver` as a dependency in your pubspec.yaml file. For example: ```yaml dependencies: - image_gallery_saver: '^2.0.2' + image_gallery_saver: '^2.0.3' ``` ## iOS diff --git a/android/build.gradle b/android/build.gradle index 4ef9cd2..b3ee973 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:7.2.0' + classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/android/src/main/kotlin/com/example/imagegallerysaver/ImageGallerySaverPlugin.kt b/android/src/main/kotlin/com/example/imagegallerysaver/ImageGallerySaverPlugin.kt index 9667881..c88c80f 100644 --- a/android/src/main/kotlin/com/example/imagegallerysaver/ImageGallerySaverPlugin.kt +++ b/android/src/main/kotlin/com/example/imagegallerysaver/ImageGallerySaverPlugin.kt @@ -1,5 +1,6 @@ package com.example.imagegallerysaver +import androidx.annotation.NonNull import android.annotation.TargetApi import android.content.ContentValues import android.content.Context @@ -26,10 +27,16 @@ import android.webkit.MimeTypeMap import java.io.OutputStream class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler { + private lateinit var methodChannel: MethodChannel private var applicationContext: Context? = null - private var methodChannel: MethodChannel? = null - override fun onMethodCall(call: MethodCall, result: Result): Unit { + override fun onAttachedToEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { + this.applicationContext = binding.applicationContext + methodChannel = MethodChannel(binding.binaryMessenger, "image_gallery_saver") + methodChannel.setMethodCallHandler(this) + } + + override fun onMethodCall(@NonNull call: MethodCall,@NonNull result: Result): Unit { when (call.method) { "saveImageToGallery" -> { val image = call.argument("imageBytes") @@ -57,13 +64,18 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler { } } + override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { + applicationContext = null + methodChannel.setMethodCallHandler(null); + } + private fun generateUri(extension: String = "", name: String? = null): Uri? { var fileName = name ?: System.currentTimeMillis().toString() + val mimeType = getMIMEType(extension) + val isVideo = mimeType?.startsWith("video")==true return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // >= android 10 - val mimeType = getMIMEType(extension) - val isVideo = mimeType?.startsWith("video")==true val uri = when { isVideo -> MediaStore.Video.Media.EXTERNAL_CONTENT_URI else -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI @@ -78,7 +90,9 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler { } ) if (!TextUtils.isEmpty(mimeType)) { - put(MediaStore.Images.Media.MIME_TYPE, mimeType) + put(when {isVideo -> MediaStore.Video.Media.MIME_TYPE + else -> MediaStore.Images.Media.MIME_TYPE + }, mimeType) } } @@ -87,7 +101,10 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler { } else { // < android 10 val storePath = - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).absolutePath + Environment.getExternalStoragePublicDirectory(when { + isVideo -> Environment.DIRECTORY_MOVIES + else -> Environment.DIRECTORY_PICTURES + }).absolutePath val appDir = File(storePath).apply { if (!exists()) { mkdir() @@ -121,10 +138,10 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler { * @param fileUri file path */ private fun sendBroadcast(context: Context, fileUri: Uri?) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - MediaScannerConnection.scanFile(context, arrayOf(fileUri?.toString()), null) { _, _ -> } - } else { - context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri)) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) + mediaScanIntent.data = fileUri + context.sendBroadcast(mediaScanIntent) } } @@ -209,29 +226,12 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler { fileInputStream?.close() } return if (success) { -// sendBroadcast(context, fileUri) + sendBroadcast(context, fileUri) SaveResultModel(fileUri.toString().isNotEmpty(), fileUri.toString(), null).toHashMap() } else { SaveResultModel(false, null, "saveFileToGallery fail").toHashMap() } } - - override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { - onAttachedToEngine(binding.applicationContext, binding.binaryMessenger) - } - - override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { - applicationContext = null - methodChannel!!.setMethodCallHandler(null); - methodChannel = null; - } - - private fun onAttachedToEngine(applicationContext: Context, messenger: BinaryMessenger) { - this.applicationContext = applicationContext - methodChannel = MethodChannel(messenger, "image_gallery_saver") - methodChannel!!.setMethodCallHandler(this) - } - } class SaveResultModel(var isSuccess: Boolean, diff --git a/example/android/build.gradle b/example/android/build.gradle index 713d7f6..f7eb7f6 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -6,7 +6,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:7.2.0' + classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 8e412ce..beb6049 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -7,7 +7,7 @@ description: Demonstrates how to use the image_gallery_saver plugin. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # Read more about versioning at semver.org. -version: 2.0.2+2 +version: 2.0.3+3 environment: sdk: '>=2.19.6 <4.0.0' diff --git a/pubspec.yaml b/pubspec.yaml index 4688d49..9ca1581 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: image_gallery_saver description: A flutter plugin for save image to gallery, iOS need to add the following keys to your Info.plist file. -version: 2.0.2 +version: 2.0.3 homepage: https://github.com/hui-z/image_gallery_saver environment: