image - Flutter:当应用程序在后台时使用路径提供程序

标签 image flutter notifications firebase-cloud-messaging flutter-method-channel

我目前正在尝试在我的 Flutter 应用程序中实现 FCM 和本地通知。我已成功配置 FCM 和普通通知的本地通知,但我还有一种不同类型的通知,我想用图像显示,当我的应用程序在前台时,通知显示没有错误,但是当我终止时应用程序/将其移动到后台我在尝试使用路径提供程序保存图像时出现异常。
异常(exception):

MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)
我假设发生此错误是因为当应用程序不在前台时路径提供程序方法 channel 已关闭,我可以做些什么来解决这个问题?或者如果不是 flutter_local_notifications插件需要位图的文件路径,我可以实现保存图像并以不同的方式获取路径以在后台工作(没有路径提供程序)吗?
(我实际上想要显示的是来自这样一个链接的图像:https://is1-ssl.mzstatic.com/image/thumb/WNUBiv2P6YSklHn9eA5nlg/1000x1000bb.jpeg)
保存图像:
 static Future<String> saveImage(Image image) {
    final completer = Completer<String>();
    image.image.resolve(ImageConfiguration()).addListener(ImageStreamListener((imageInfo,_) async {
      final byteData = await imageInfo.image.toByteData(format: ImageByteFormat.png);
      final pngBytes = byteData.buffer.asUint8List();
      final fileName = pngBytes.hashCode;
      final directory = await getApplicationDocumentsDirectory();
      final filePath = '${directory.path}/$fileName';
      final file = File(filePath);
      await file.writeAsBytes(pngBytes);
      completer.complete(filePath);
    }));
    return completer.future;
  }

最佳答案

您还需要在 Application.java 中注册路径提供程序。

import io.flutter.plugins.pathprovider.PathProviderPlugin;

...

    @Override
    public void registerWith(PluginRegistry registry) {
PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"));
    }
现在 Flutter 使用 Kotlin 作为 android 端的默认语言,这里是 Kotlin 代码:
override fun registerWith(registry: PluginRegistry) {
    io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"))
}

关于image - Flutter:当应用程序在后台时使用路径提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62623763/

相关文章:

php - 检查图像文件类型

gridview - 没有名为 'itemCount' 的命名参数。在 Flutter GridView 中

android - 如何使Firebase用户在Google登录功能之外 flutter 朔迷离?

Android 仅在状态栏展开时显示通知

android - 我的应用程序无法在 API 23 中运行

html - IE9 中的图像模糊、内容移动、无法重现的错误

html - 如何将按钮/图像 onclick 更改为事件状态?

移动图像后,HTML + CSS 图像链接将不起作用

flutter - 如何在扩展面板的正文中显示列表?

android - NameError: uninitialized constant Rpush::Gcm 我正在尝试将 rpush 用于我的移动应用程序。但我一直收到这个错误