1
1
package com.example.imagegallerysaver
2
2
3
+ import androidx.annotation.NonNull
3
4
import android.annotation.TargetApi
4
5
import android.content.ContentValues
5
6
import android.content.Context
@@ -26,10 +27,16 @@ import android.webkit.MimeTypeMap
26
27
import java.io.OutputStream
27
28
28
29
class ImageGallerySaverPlugin : FlutterPlugin , MethodCallHandler {
30
+ private lateinit var methodChannel: MethodChannel
29
31
private var applicationContext: Context ? = null
30
- private var methodChannel: MethodChannel ? = null
31
32
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 {
33
40
when (call.method) {
34
41
" saveImageToGallery" -> {
35
42
val image = call.argument<ByteArray ?>(" imageBytes" )
@@ -57,13 +64,18 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
57
64
}
58
65
}
59
66
67
+ override fun onDetachedFromEngine (@NonNull binding : FlutterPlugin .FlutterPluginBinding ) {
68
+ applicationContext = null
69
+ methodChannel.setMethodCallHandler(null );
70
+ }
71
+
60
72
private fun generateUri (extension : String = "", name : String? = null): Uri ? {
61
73
var fileName = name ? : System .currentTimeMillis().toString()
74
+ val mimeType = getMIMEType(extension)
75
+ val isVideo = mimeType?.startsWith(" video" )== true
62
76
63
77
return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
64
78
// >= android 10
65
- val mimeType = getMIMEType(extension)
66
- val isVideo = mimeType?.startsWith(" video" )== true
67
79
val uri = when {
68
80
isVideo -> MediaStore .Video .Media .EXTERNAL_CONTENT_URI
69
81
else -> MediaStore .Images .Media .EXTERNAL_CONTENT_URI
@@ -78,7 +90,9 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
78
90
}
79
91
)
80
92
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)
82
96
}
83
97
}
84
98
@@ -87,7 +101,10 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
87
101
} else {
88
102
// < android 10
89
103
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
91
108
val appDir = File (storePath).apply {
92
109
if (! exists()) {
93
110
mkdir()
@@ -121,10 +138,10 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
121
138
* @param fileUri file path
122
139
*/
123
140
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 )
128
145
}
129
146
}
130
147
@@ -209,29 +226,12 @@ class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
209
226
fileInputStream?.close()
210
227
}
211
228
return if (success) {
212
- // sendBroadcast(context, fileUri)
229
+ sendBroadcast(context, fileUri)
213
230
SaveResultModel (fileUri.toString().isNotEmpty(), fileUri.toString(), null ).toHashMap()
214
231
} else {
215
232
SaveResultModel (false , null , " saveFileToGallery fail" ).toHashMap()
216
233
}
217
234
}
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
-
235
235
}
236
236
237
237
class SaveResultModel (var isSuccess : Boolean ,
0 commit comments