Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not work on flutter 2.5,about Android V2 embedding #186

Closed
hu70258tao opened this issue Sep 9, 2021 · 9 comments
Closed

can not work on flutter 2.5,about Android V2 embedding #186

hu70258tao opened this issue Sep 9, 2021 · 9 comments

Comments

@hu70258tao
Copy link

The plugin image_gallery_saver uses a deprecated version of the Android embedding.
To avoid unexpected runtime failures, or future build failures, try to see if this plugin supports the Android V2 embedding. Otherwise, consider removing it since a future release of Flutter will remove these deprecated APIs.
If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.

@basnetjiten
Copy link

The plugins `image_gallery_saver use a deprecated version of the Android embedding.
To avoid unexpected runtime failures, or future build failures, try to see if these plugins support the Android V2 embedding. Otherwise, consider removing them since a future release of Flutter will remove these deprecated APIs.
If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.

@hu70258tao
Copy link
Author

完了,上次作者提交代码都是四月的事了.我看八天前分支里有人已经修正了该错误,请求作者进行合并到master,但是一直没有动静.

@darshakpranpariya
Copy link

darshakpranpariya commented Sep 9, 2021

I am getting this warning,
https://user-images.githubusercontent.com/34344691/132642394-bf527f27-d095-416f-bd95-0d9cef7e2bff.png
Might be this is the cause for me to fail the build.
Please, anybody knows that how to fix this.
P.S. :- I upgraded my flutter SDK to 2.5.0.

@hu70258tao
Copy link
Author

I am getting this warning,
https://user-images.githubusercontent.com/34344691/132642394-bf527f27-d095-416f-bd95-0d9cef7e2bff.png
Might be this is the cause for me to fail the build.
Please, anybody knows that how to fix this.
P.S. :- I upgraded my flutter SDK to 2.5.0.

你好,我不懂英语.这个错误非常严重,是因为库使用了过时的API,而Flutter2.5已经完全删除了.根据你的截图,有好几个库都出现了这个问题,需要等作者来修改才行.本库的解决方案是,替换掉对应的文件,以下代码

package com.example.imagegallerysaver

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Environment
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
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.PluginRegistry.Registrar
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
    private var applicationContext: Context? = null
    private var methodChannel: MethodChannel? = null


    companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar) {
            val instance = ImageGallerySaverPlugin()
            instance.onAttachedToEngine(registrar.context(), registrar.messenger())
        }
    }

    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")

                result.success(saveImageToGallery(BitmapFactory.decodeByteArray(image, 0, image.size), quality, name))
            }
            call.method == "saveFileToGallery" -> {
                val path = call.argument<String>("file") ?: return
                val name = call.argument<String>("name")
                result.success(saveFileToGallery(path, name))
            }
            else -> result.notImplemented()
        }

    }


    private fun generateFile(extension: String = "", name: String? = null): File {
        val storePath = Environment.getExternalStorageDirectory().absolutePath + File.separator + Environment.DIRECTORY_PICTURES
        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?): HashMap<String, Any?> {
        val context = applicationContext
        val file = generateFile("jpg", name = name)
        return 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()
            SaveResultModel(uri.toString().isNotEmpty(), uri.toString(), null).toHashMap()
        } catch (e: IOException) {
            SaveResultModel(false, null, e.toString()).toHashMap()
        }
    }

    private fun saveFileToGallery(filePath: String, name: String?): HashMap<String, Any?> {
        val context = applicationContext
        return try {
            val originalFile = File(filePath)
            val file = generateFile(originalFile.extension, name)
            originalFile.copyTo(file)

            val uri = Uri.fromFile(file)
            context!!.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri))
            SaveResultModel(uri.toString().isNotEmpty(), uri.toString(), null).toHashMap()
        } catch (e: IOException) {
            SaveResultModel(false, null, e.toString()).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,
                      var filePath: String? = null,
                      var errorMessage: String? = null) {
    fun toHashMap(): HashMap<String, Any?> {
        val hashMap = HashMap<String, Any?>()
        hashMap["isSuccess"] = isSuccess
        hashMap["filePath"] = filePath
        hashMap["errorMessage"] = errorMessage
        return hashMap
    }
}

@darshakpranpariya
Copy link

Thanks for your response :)

@zakblacki
Copy link

I am getting this warning,
https://user-images.githubusercontent.com/34344691/132642394-bf527f27-d095-416f-bd95-0d9cef7e2bff.png
Might be this is the cause for me to fail the build.
Please, anybody knows that how to fix this.
P.S. :- I upgraded my flutter SDK to 2.5.0.

你好,我不懂英语.这个错误非常严重,是因为库使用了过时的API,而Flutter2.5已经完全删除了.根据你的截图,有好几个库都出现了这个问题,需要等作者来修改才行.本库的解决方案是,替换掉对应的文件,以下代码

package com.example.imagegallerysaver

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Environment
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
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.PluginRegistry.Registrar
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


class ImageGallerySaverPlugin : FlutterPlugin, MethodCallHandler {
    private var applicationContext: Context? = null
    private var methodChannel: MethodChannel? = null


    companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar) {
            val instance = ImageGallerySaverPlugin()
            instance.onAttachedToEngine(registrar.context(), registrar.messenger())
        }
    }

    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")

                result.success(saveImageToGallery(BitmapFactory.decodeByteArray(image, 0, image.size), quality, name))
            }
            call.method == "saveFileToGallery" -> {
                val path = call.argument<String>("file") ?: return
                val name = call.argument<String>("name")
                result.success(saveFileToGallery(path, name))
            }
            else -> result.notImplemented()
        }

    }


    private fun generateFile(extension: String = "", name: String? = null): File {
        val storePath = Environment.getExternalStorageDirectory().absolutePath + File.separator + Environment.DIRECTORY_PICTURES
        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?): HashMap<String, Any?> {
        val context = applicationContext
        val file = generateFile("jpg", name = name)
        return 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()
            SaveResultModel(uri.toString().isNotEmpty(), uri.toString(), null).toHashMap()
        } catch (e: IOException) {
            SaveResultModel(false, null, e.toString()).toHashMap()
        }
    }

    private fun saveFileToGallery(filePath: String, name: String?): HashMap<String, Any?> {
        val context = applicationContext
        return try {
            val originalFile = File(filePath)
            val file = generateFile(originalFile.extension, name)
            originalFile.copyTo(file)

            val uri = Uri.fromFile(file)
            context!!.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri))
            SaveResultModel(uri.toString().isNotEmpty(), uri.toString(), null).toHashMap()
        } catch (e: IOException) {
            SaveResultModel(false, null, e.toString()).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,
                      var filePath: String? = null,
                      var errorMessage: String? = null) {
    fun toHashMap(): HashMap<String, Any?> {
        val hashMap = HashMap<String, Any?>()
        hashMap["isSuccess"] = isSuccess
        hashMap["filePath"] = filePath
        hashMap["errorMessage"] = errorMessage
        return hashMap
    }
}

Where do we put this code replace it ?

@ahguerraViridian
Copy link

ahguerraViridian commented Sep 20, 2021

Hi! I just checked the github code and it looks like the v2 embedding was indeed implemented. We should only have to wait for a new version. It's been 19 days since the fix though. The fix is even older than this report.

Another option is to reference the git project like this:

image_gallery_saver:
    git:
      url: https://github.com/hui-z/image_gallery_saver.git
      ref: master

Just tested it, and the warning dissapears.

@inc16sec
Copy link

Hi! I just checked the github code and it looks like the v2 embedding was indeed implemented. We should only have to wait for a new version. It's been 19 days since the fix though. The fix is even older than this report.

Another option is to reference the git project like this:

image_gallery_saver:
    git:
      url: https://github.com/hui-z/image_gallery_saver.git
      ref: master

Just tested it, and the warning dissapears.

I can confirm. After trying this, it did work as intended.
Thank you @ahguerraViridian

@ahguerraViridian
Copy link

Version 1.7.0 that includes the fix is now released!

@xqqlv xqqlv closed this as completed Nov 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants