Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
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
  • Loading branch information
woodwen committed Jul 3, 2023
1 parent 6264458 commit bc16438
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.imagegallerysaver

import androidx.annotation.NonNull
import android.annotation.TargetApi
import android.content.ContentValues
import android.content.Context
Expand All @@ -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<ByteArray?>("imageBytes")
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit bc16438

Please sign in to comment.