Skip to content

Commit 8c6480b

Browse files
authored
Merge pull request #261 from woodwen/master
Update
2 parents 7b85ced + bc16438 commit 8c6480b

File tree

7 files changed

+38
-33
lines changed

7 files changed

+38
-33
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.0.3
2+
- 1.Upgrade flutter version to 3.10.5
3+
- 2.Android build tools are upgraded to 7.3.0
4+
- 3.Optimize the Android plugin library code
5+
16
## 2.0.2
27
- 1.Optimization android plugin
38

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ We use the `image_picker` plugin to select images from the Android and iOS image
1111
To use this plugin, add `image_gallery_saver` as a dependency in your pubspec.yaml file. For example:
1212
```yaml
1313
dependencies:
14-
image_gallery_saver: '^2.0.2'
14+
image_gallery_saver: '^2.0.3'
1515
```
1616
1717
## iOS

android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
}
1010

1111
dependencies {
12-
classpath 'com.android.tools.build:gradle:7.2.0'
12+
classpath 'com.android.tools.build:gradle:7.3.0'
1313
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1414
}
1515
}

android/src/main/kotlin/com/example/imagegallerysaver/ImageGallerySaverPlugin.kt

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.imagegallerysaver
22

3+
import androidx.annotation.NonNull
34
import android.annotation.TargetApi
45
import android.content.ContentValues
56
import android.content.Context
@@ -26,10 +27,16 @@ import android.webkit.MimeTypeMap
2627
import java.io.OutputStream
2728

2829
class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
30+
private lateinit var methodChannel: MethodChannel
2931
private var applicationContext: Context? = null
30-
private var methodChannel: MethodChannel? = null
3132

32-
override fun onMethodCall(call: MethodCall, result: Result): Unit {
33+
override fun onAttachedToEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
34+
this.applicationContext = binding.applicationContext
35+
methodChannel = MethodChannel(binding.binaryMessenger, "image_gallery_saver")
36+
methodChannel.setMethodCallHandler(this)
37+
}
38+
39+
override fun onMethodCall(@NonNull call: MethodCall,@NonNull result: Result): Unit {
3340
when (call.method) {
3441
"saveImageToGallery" -> {
3542
val image = call.argument<ByteArray?>("imageBytes")
@@ -57,13 +64,18 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
5764
}
5865
}
5966

67+
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
68+
applicationContext = null
69+
methodChannel.setMethodCallHandler(null);
70+
}
71+
6072
private fun generateUri(extension: String = "", name: String? = null): Uri? {
6173
var fileName = name ?: System.currentTimeMillis().toString()
74+
val mimeType = getMIMEType(extension)
75+
val isVideo = mimeType?.startsWith("video")==true
6276

6377
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
6478
// >= android 10
65-
val mimeType = getMIMEType(extension)
66-
val isVideo = mimeType?.startsWith("video")==true
6779
val uri = when {
6880
isVideo -> MediaStore.Video.Media.EXTERNAL_CONTENT_URI
6981
else -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI
@@ -78,7 +90,9 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
7890
}
7991
)
8092
if (!TextUtils.isEmpty(mimeType)) {
81-
put(MediaStore.Images.Media.MIME_TYPE, mimeType)
93+
put(when {isVideo -> MediaStore.Video.Media.MIME_TYPE
94+
else -> MediaStore.Images.Media.MIME_TYPE
95+
}, mimeType)
8296
}
8397
}
8498

@@ -87,7 +101,10 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
87101
} else {
88102
// < android 10
89103
val storePath =
90-
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).absolutePath
104+
Environment.getExternalStoragePublicDirectory(when {
105+
isVideo -> Environment.DIRECTORY_MOVIES
106+
else -> Environment.DIRECTORY_PICTURES
107+
}).absolutePath
91108
val appDir = File(storePath).apply {
92109
if (!exists()) {
93110
mkdir()
@@ -121,10 +138,10 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
121138
* @param fileUri file path
122139
*/
123140
private fun sendBroadcast(context: Context, fileUri: Uri?) {
124-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
125-
MediaScannerConnection.scanFile(context, arrayOf(fileUri?.toString()), null) { _, _ -> }
126-
} else {
127-
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri))
141+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
142+
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
143+
mediaScanIntent.data = fileUri
144+
context.sendBroadcast(mediaScanIntent)
128145
}
129146
}
130147

@@ -209,29 +226,12 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
209226
fileInputStream?.close()
210227
}
211228
return if (success) {
212-
// sendBroadcast(context, fileUri)
229+
sendBroadcast(context, fileUri)
213230
SaveResultModel(fileUri.toString().isNotEmpty(), fileUri.toString(), null).toHashMap()
214231
} else {
215232
SaveResultModel(false, null, "saveFileToGallery fail").toHashMap()
216233
}
217234
}
218-
219-
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
220-
onAttachedToEngine(binding.applicationContext, binding.binaryMessenger)
221-
}
222-
223-
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
224-
applicationContext = null
225-
methodChannel!!.setMethodCallHandler(null);
226-
methodChannel = null;
227-
}
228-
229-
private fun onAttachedToEngine(applicationContext: Context, messenger: BinaryMessenger) {
230-
this.applicationContext = applicationContext
231-
methodChannel = MethodChannel(messenger, "image_gallery_saver")
232-
methodChannel!!.setMethodCallHandler(this)
233-
}
234-
235235
}
236236

237237
class SaveResultModel(var isSuccess: Boolean,

example/android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
}
77

88
dependencies {
9-
classpath 'com.android.tools.build:gradle:7.2.0'
9+
classpath 'com.android.tools.build:gradle:7.3.0'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111
}
1212
}

example/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Demonstrates how to use the image_gallery_saver plugin.
77
# Both the version and the builder number may be overridden in flutter
88
# build by specifying --build-name and --build-number, respectively.
99
# Read more about versioning at semver.org.
10-
version: 2.0.2+2
10+
version: 2.0.3+3
1111

1212
environment:
1313
sdk: '>=2.19.6 <4.0.0'

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: image_gallery_saver
22
description: A flutter plugin for save image to gallery, iOS need to add the following keys to your Info.plist file.
3-
version: 2.0.2
3+
version: 2.0.3
44
homepage: https://github.com/hui-z/image_gallery_saver
55

66
environment:

0 commit comments

Comments
 (0)