Last active
July 1, 2021 18:40
-
-
Save olumidayy/1d23ae2dcdec7e77a289e9203e702437 to your computer and use it in GitHub Desktop.
A modification to the image_gallery_saver plugin that allows preferred savePath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.imagegallerysaver | |
import android.content.Intent | |
import android.content.pm.ApplicationInfo | |
import android.content.pm.PackageManager | |
import android.graphics.Bitmap | |
import android.graphics.BitmapFactory | |
import android.net.Uri | |
import android.os.Environment | |
import io.flutter.plugin.common.MethodChannel | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | |
import io.flutter.plugin.common.MethodChannel.Result | |
import io.flutter.plugin.common.MethodCall | |
import io.flutter.plugin.common.PluginRegistry.Registrar | |
import java.io.File | |
import java.io.FileOutputStream | |
import java.io.IOException | |
class ImageGallerySaverPlugin(private val registrar: Registrar): MethodCallHandler { | |
companion object { | |
@JvmStatic | |
fun registerWith(registrar: Registrar) { | |
val channel = MethodChannel(registrar.messenger(), "image_gallery_saver") | |
channel.setMethodCallHandler(ImageGallerySaverPlugin(registrar)) | |
} | |
} | |
override fun onMethodCall(call: MethodCall, result: Result): Unit { | |
when { | |
call.method == "saveImageToGallery" -> { | |
val image = call.argument<ByteArray>("imageBytes") ?: return | |
val quality = call.argument<Int>("quality") ?: return | |
val name = call.argument<String>("name") | |
val storePath = call.argument<String>("storePath") | |
result.success(saveImageToGallery(BitmapFactory.decodeByteArray(image,0,image.size), quality, name, storePath)) | |
} | |
call.method == "saveFileToGallery" -> { | |
val path = call.argument<String>("path") as String | |
val storePath = call.argument<String>("storePath") | |
val name = call.argument<String>("name") | |
result.success(saveFileToGallery(path, name, storePath)) | |
} | |
else -> result.notImplemented() | |
} | |
} | |
private fun generateFile(extension: String = "", name: String? = null, storePath: String? = null): File { | |
val storePath = storePath ?: Environment.getExternalStorageDirectory().absolutePath + File.separator + getApplicationName() | |
val appDir = File(storePath) | |
if (!appDir.exists()) { | |
appDir.mkdir() | |
} | |
var fileName = name?:System.currentTimeMillis().toString() | |
if (extension.isNotEmpty()) { | |
fileName += (".$extension") | |
} | |
return File(appDir, fileName) | |
} | |
private fun saveImageToGallery(bmp: Bitmap, quality: Int, name: String?, storePath: String?): String { | |
val context = registrar.activeContext().applicationContext | |
val file = generateFile("jpg", name = name, storePath = storePath) | |
try { | |
val fos = FileOutputStream(file) | |
println("ImageGallerySaverPlugin $quality") | |
bmp.compress(Bitmap.CompressFormat.JPEG, quality, fos) | |
fos.flush() | |
fos.close() | |
val uri = Uri.fromFile(file) | |
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)) | |
bmp.recycle() | |
return uri.toString() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
return "" | |
} | |
private fun saveFileToGallery(filePath: String, name: String?, storePath: String?): String { | |
val context = registrar.activeContext().applicationContext | |
return try { | |
val originalFile = File(filePath) | |
val file = generateFile(originalFile.extension, storePath = storePath) | |
originalFile.copyTo(file) | |
val uri = Uri.fromFile(file) | |
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)) | |
return uri.toString() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
"" | |
} | |
} | |
private fun getApplicationName(): String { | |
val context = registrar.activeContext().applicationContext | |
var ai: ApplicationInfo? = null | |
try { | |
ai = context.packageManager.getApplicationInfo(context.packageName, 0) | |
} catch (e: PackageManager.NameNotFoundException) { | |
} | |
var appName: String | |
appName = if (ai != null) { | |
val charSequence = context.packageManager.getApplicationLabel(ai) | |
StringBuilder(charSequence.length).append(charSequence).toString() | |
} else { | |
"image_gallery_saver" | |
} | |
return appName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment